Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:26:34+00:00 2026-05-28T00:26:34+00:00

This is easier to explain with some code so I’ll give an example first:

  • 0

This is easier to explain with some code so I’ll give an example first:

#include <iostream>
#include <vector>

class Base {
public:
    int integer;

    Base() : integer(0) {}
    Base(int i) : integer(i) {}
};

class Double: public Base {
public:
    Double(int i) { integer = i * 2; }
};

class Triple: public Base {
public:
    Triple(int i) { integer = i * 3; }
};

template<typename T>
Base* createBaseObject(int i) {
    return new T(i);
};

int main() {
    std::vector<Base*> objects;

    objects.push_back(createBaseObject<Double>(2));
    objects.push_back(createBaseObject<Triple>(2));

    for(int i = 0; i < objects.size(); ++i) {
        std::cout << objects[i]->integer << std::endl;
    }

    std::cin.get();
    return 0;
}

I am trying to make a function that will return a Base pointer to an object that is derived from Base. In the above code the function createBaseObject allows me to do that but it restricts me in that it can only create dervied classes that take a single argument into their constructor.

For example if I wanted to make a derived class Multiply:

class Multiply: public Base {
public:
    Multiply(int i, int amount) { integer = i * amount; }
};

createBaseObject wouldn’t be able to create a Multiply object as it’s constructor takes two arguments.

I want to ultimately do something like this:

struct BaseCreator {
    typedef Base* (*funcPtr)(int);
    BaseCreator(std::string name, funcPtr f) : identifier(name), func(f) {}

    std::string identifier;
    funcPtr func;
};

then, for example, when I get input matching identifier I can create a new object of whatever derived class associates with that identifier with whatever arguments were input too and push it to the container.


After reading some of the replies I think something like this would suit my needs to be able to procedurally create an instance of an object? I’m not too wise with templates though so I do not know whether this is legal.

struct CreatorBase {
    std::string identifier;
    CreatorBase(std::string name) : identifier(name) {}

    template<typename... Args>
    virtual Base* createObject(Args... as) = 0;
};

template<typename T>
struct Creator: public CreatorBase {
    typedef T type;

    template<typename... Args>
    Base* createObject(Args... as) {
        return new type(as...);
    }
};

Okay here’s another semi-solution I’ve managed to come up with so far:

#include <boost\lambda\bind.hpp>
#include <boost\lambda\construct.hpp>
#include <boost\function.hpp>

using namespace boost::lambda;
boost::function<Base(int)> dbl = bind(constructor<Double>(), _1);
boost::function<Base(int, int)> mult = bind(constructor<Multiply>(), _1, _2);

Just this has the same limits as the original in that I can’t have a single pointer that will point to both dbl and mult.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:26:34+00:00Added an answer on May 28, 2026 at 12:26 am

    C++11 variadic templates can do this for you.

    You already have your new derived class:

    class Multiply: public Base {
    public:
        Multiply(int i, int amount) { integer = i * amount; }
    };
    

    Then change your factory:

    template<typename T, typename... Args>
    Base* createBaseObject(Args... as) {
        return new T(as...);
    };
    

    And, finally, allow the arguments to be deduced:

    objects.push_back(createBaseObject<Multiply>(3,4));
    

    Live demo.


    As others have said, though, it does all seem a little pointless. Presumably your true use case is less contrived.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is easier to explain with an example. Given these two classes: public class
I guess it's easier to explain using an example: HTML: <div class=boxset> <div class=box_header>
Alright, Ill try to explain this as best as I can first... My program
Suppose you have to implement Graph class, containing some algorithms, using dfs (depth-first search).
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
This is probably easier than I am making it, but basically what I need
I figure this problem is easier than just a regular spell checker since the
This is hard for me to word, but easier for me to demonstrate: I
Why is this archaic format still used in the face of easier-to-use technologies? Does
I get this error: Can't locate Foo.pm in @INC Is there an easier way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.