fork(5) download
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <functional>
  5.  
  6. using namespace std;
  7.  
  8. struct BaseClass {virtual void foo()=0;};
  9. struct DerivedClass1 : public BaseClass {void foo() {cout << "1" << endl;}};
  10. struct DerivedClass2 : public BaseClass {void foo() {cout << "2" << endl;}};
  11.  
  12. map<string,function<BaseClass*()> > factory {
  13. {"one", [](){return new DerivedClass1();}}
  14. , {"two", [](){return new DerivedClass2();}}
  15. };
  16.  
  17. int main() {
  18. BaseClass *a = factory["one"]();
  19. BaseClass *b = factory["two"]();
  20. a->foo();
  21. b->foo();
  22. delete a;
  23. delete b;
  24. return 0;
  25. }
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
1
2