Is there a way to instantiate objects from a string holding their class name?
I got a problem basically the same with the above question. But I need to instantiate a class with some parameters. different class constructor name may require different number of variables and the type of each variable may differ either. And because the class contains constant variables so you could not new it with new T() and then set the parameter to the correct value. the “class name”-“constructor” map seems not suitable for my needs.
Any alternatives?
I’m going to preface this by saying, maybe C++ isn’t the right language for whatever you’re trying to accomplish. The more you try to control your program via external data, the closer you get to full on scripting. And there are lots of more powerful options when that is your goal.
That said, sure, it is possible, although not as easily. Two ways come immediately to mind. The first is to require all applicable types to have a constructor that accepts a
std::string. This constructor will be responsible for its own parsing.If you don’t want to change your type definitions, or this is otherwise unacceptable [perhaps your types already have such a constructor?], get rid of the templated
createInstancemethod, and provide individual versions for each type you are interested in. This function does the parsing, and calls the appropriate constructor.A third option might be possible using variadic templates [or boost-esque variadic-like templates] that would bring you back to the original simplicity.
However, I have no idea how to pull that off, or even if it is possible.