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: 
If when you create an object, you already know its parameters, and that it will be a Fish, you don't need a factory at all: just construct a Fish and you are done. 

You can reasonably make use of a factory in case you don't know which object will result out of it, from the caller. For example you give as input of the factory method a string, maybe read from a file: the factory creates and returns the proper object type by parsing the string.

The caller don't know whether it will be a Fish or a Dog: that's the aim of the factory method.

And moreover you make use of a factory when you can extend it by adding more "constructable" objects by inheritance and overriding the virtual creationist method. That doesn't happen in case the methods have different signatures - they are in fact different methods
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.