fork(1) download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <unordered_map>
  4. using namespace std;
  5.  
  6. struct base { base(){} virtual ~base(){} };
  7. struct A:public base { A(){} };
  8. struct B:public base { B(){} };
  9. struct C:public base { C(){} };
  10. typedef base *Creaton();
  11. template<class T> base *creaton() { return new T; };
  12.  
  13. int main()
  14. {
  15. unordered_map<string,Creaton*> tb={{{"a",creaton<A>},{"b",creaton<B>},{"c",creaton<C>}}};
  16. base *tmp;
  17.  
  18. tmp=tb["b"]();
  19. cout<<typeid(*tmp).name()<<endl;
  20. delete tmp;
  21.  
  22. tmp=tb["a"]();
  23. cout<<typeid(*tmp).name()<<endl;
  24. delete tmp;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
1B
1A