fork(4) download
  1. #include <map>
  2. #include <string>
  3.  
  4. class Base {};
  5. class MyClass1 : public Base {};
  6. class MyClass2 : public Base {};
  7. class MyClass3 : public Base {};
  8. //...etc...
  9.  
  10. std::map<std::string, Base *(*)()> Factory;
  11. #define MakeInstanceGetter(clazz) \
  12. Base *Make_##clazz(){ return new clazz; } \
  13. Base *(*ignore_##clazz##_clutter)() = Factory[#clazz] = &Make_##clazz
  14. MakeInstanceGetter(MyClass1);
  15. MakeInstanceGetter(MyClass2);
  16. MakeInstanceGetter(MyClass3);
  17. //...etc...
  18.  
  19. int main()
  20. {
  21. Base *obj = Factory["MyClass2"]();
  22. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Standard output is empty