#include <map>
#include <string>
class Base {};
class MyClass1 : public Base {};
class MyClass2 : public Base {};
class MyClass3 : public Base {};
//...etc...
std::map<std::string, Base *(*)()> Factory;
#define MakeInstanceGetter(clazz) \
Base *Make_##clazz(){ return new clazz; } \
Base *(*ignore_##clazz##_clutter)() = Factory[#clazz] = &Make_##clazz
MakeInstanceGetter(MyClass1);
MakeInstanceGetter(MyClass2);
MakeInstanceGetter(MyClass3);
//...etc...
int main()
{
Base *obj = Factory["MyClass2"]();
}
I2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHN0cmluZz4KCmNsYXNzIEJhc2Uge307CmNsYXNzIE15Q2xhc3MxIDogcHVibGljIEJhc2Uge307CmNsYXNzIE15Q2xhc3MyIDogcHVibGljIEJhc2Uge307CmNsYXNzIE15Q2xhc3MzIDogcHVibGljIEJhc2Uge307Ci8vLi4uZXRjLi4uCgpzdGQ6Om1hcDxzdGQ6OnN0cmluZywgQmFzZSAqKCopKCk+IEZhY3Rvcnk7CiNkZWZpbmUgTWFrZUluc3RhbmNlR2V0dGVyKGNsYXp6KSBcCkJhc2UgKk1ha2VfIyNjbGF6eigpeyByZXR1cm4gbmV3IGNsYXp6OyB9IFwKQmFzZSAqKCppZ25vcmVfIyNjbGF6eiMjX2NsdXR0ZXIpKCkgPSBGYWN0b3J5WyNjbGF6el0gPSAmTWFrZV8jI2NsYXp6Ck1ha2VJbnN0YW5jZUdldHRlcihNeUNsYXNzMSk7Ck1ha2VJbnN0YW5jZUdldHRlcihNeUNsYXNzMik7Ck1ha2VJbnN0YW5jZUdldHRlcihNeUNsYXNzMyk7Ci8vLi4uZXRjLi4uCgppbnQgbWFpbigpCnsKCUJhc2UgKm9iaiA9IEZhY3RvcnlbIk15Q2xhc3MyIl0oKTsKfQ==