fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <functional>
  4.  
  5. struct A
  6. {
  7. virtual void f() = 0;
  8. };
  9.  
  10. struct B : public A
  11. {
  12. void f() { std::cout << "B::f\n"; }
  13. };
  14.  
  15. struct C : public A
  16. {
  17. void f() { std::cout << "C::f\n"; }
  18. };
  19.  
  20. B* getB() { return new B; }
  21. C* getC() { return new C; }
  22.  
  23. int main()
  24. {
  25. std::map<std::string, std::function<A*()>> m;
  26. m["b"] = getB;
  27. m["c"] = getC;
  28.  
  29. m["b"]()->f();
  30. m["c"]()->f();
  31. }
  32.  
Success #stdin #stdout 0s 3068KB
stdin
Standard input is empty
stdout
B::f
C::f