I have several subclasses of one superclass and want to create an instance of a specific class according to a given string
Superclass instantiateSubclass(string s);
Instead of having a huge if-cascade I want to work with a config file to accomplish this.
This allows me to change the possible values for string s without recompiling and I hope it leads to a more concise code.
The config file should contain strings like “subclass1”, “subclass2”, but how do I create the class according to a string then?
Basically I need a mapping from string to class, is this possible in C++? I think other languages offer possibilities like reflection for this problem.
Register your classes:
To make: