Question: I want to have a class that creates different sorts of objects based on a string I pass it. From my research, this best describes the Factory design pattern. I am having success implementing it, but I've run into a design issue: I don't know how to create objects with different length constructors.

Let's take for example, an abstract parent class called Pet. From it are 3 children: Fish, Cat, and Dog. They all inherit weight and color from Pet, so that goes in their constructors. But a fish might want a number of fins and a boolean regarding whether it is a saltwater fish. That's a 4 parameter constructor. Cat would like number of legs. That's 3 parameters. The dog might have parameters for legs, breed, and whether he plays well with other dogs, for 5 parameters.

In C++, I understand that there isn't any reflection, so the most common practice seems to be to just declare a map of string to function pointers, where the function pointer points to a function that looks something like this:

```
    template<typename T> Pet* createObject(int weight, std::string color) {return new T(weight, color);}

```

Again, I'm not sure how I would stuff more parameters into the call without affecting the calls of other objects' constructors.

I can think of two workarounds: make new functions to accept different amount of parameters or make default parameters for constructors above a certain size.

Workaround 1 seems kind of excessive depending on how many different parameter sizes I have.

Workaround 2 seems to disregard the entire point of a constructor, as I will be forced to assign data after calling the constructor.

Are there any other better workarounds?

Answer: 
Is this what you need (forgive memory leaks and the like)

```
#include <map>
#include <string>

// definition of pet hierarcy
class pet_t
{
public:
    virtual ~pet_t(void) {}
};

class frog_t : public pet_t
{
public:
    frog_t(int) {}
    static frog_t *builder(int n) { return new frog_t(n); }
};

class dog_t : public pet_t
{
public:
    dog_t(const char *, int) {}
    static dog_t *builder(const char *n, int p) { return new dog_t(n, p); }
};
// the per builder function type
typedef pet_t *(*pet_builder_t)(...);
// the map containing per builders: it's indexed by per type name
std::map<std::string, pet_builder_t> registry;
void build_factory(void)
{
    registry["frog"] = reinterpret_cast<pet_builder_t>(&frog_t::builder);
    registry["dog"] = reinterpret_cast<pet_builder_t>(&dog_t::builder);
}
// the actual factory function
template <class ...Ts>
pet_t *factory(std::string name, Ts&&...ts)
{
    pet_builder_t builder = registry[name];
    // assume there is something in the map
    return builder(std::forward<Ts>(ts)...);
}

int main(int argc, char *argv[])
{
    build_factory();
    dog_t  *dog  = dynamic_cast<dog_t  *>(factory(std::string("dog"), std::string("pluto"), 3));
    frog_t *frog = dynamic_cast<frog_t *>(factory(std::string("frog"), 7));
}

```

I feel like there are too many casts but the idea should be a good starting point.
There are many approaches to this problem; the one I prefer may not be the most elegant, but it is extremely explicit. Basically, you create a map to `boost::any` pointers, and all constructors now simply take such a map.

```
using MyArgs = unordered_map<string, boost::any>;

class Fish {
  Fish(MyArgs args) {
    int num_fins = boost::any_cast<int>(args.at("num_fins"));
  }

```

Now all of your constructors have the same signature, so your factory can just look this like:

```
unique_ptr<Pet> factory(string animal_name, MyArgs args) {
  auto func = factory_map.at(animal_name);
  return func(args);
}

```

Edit: I should also note that if you make a mistake, that is your MyArgs is missing an argument or has one of the wrong type, an exception will be thrown. So you can get a nice clear error and even handle it, as opposed to getting UB.