fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. struct A {
  6. virtual ~A(){}
  7. };
  8.  
  9. struct B : A
  10. {
  11. B()
  12. {
  13. std::cout << "B constructed\n";
  14. }
  15.  
  16. virtual ~B()
  17. {
  18. std::cout << "B destructed\n";
  19. }
  20. };
  21.  
  22. struct C : A
  23. {
  24. C()
  25. {
  26. std::cout << "C constructed\n";
  27. }
  28.  
  29. virtual ~C()
  30. {
  31. std::cout << "C destructed\n";
  32. }
  33. };
  34.  
  35. template <class T>
  36. A* create()
  37. {
  38. return new T;
  39. }
  40.  
  41. typedef A*(*CreateFN)();
  42.  
  43.  
  44. int main()
  45. {
  46. std::map<std::string, CreateFN> classMap;
  47. classMap["B"] = &create<B>;
  48. classMap["C"] = &create<C>;
  49.  
  50. delete classMap["B"]();
  51. delete classMap["C"]();
  52. }
  53.  
Success #stdin #stdout 0.02s 2820KB
stdin
Standard input is empty
stdout
B constructed
B destructed
C constructed
C destructed