fork download
  1. #include <iostream>
  2.  
  3. struct Base
  4. {
  5. virtual void mem_f() = 0;
  6. virtual ~Base() {};
  7. };
  8.  
  9. template <class T>
  10. struct Derived;
  11.  
  12. template <>
  13. struct Derived<std::string> : public Base
  14. {
  15. virtual void mem_f() { std::cout << "Derived<std::string>::mem_f()\n"; };
  16. };
  17.  
  18. template <>
  19. struct Derived<int> : public Base
  20. {
  21. virtual void mem_f() { std::cout << "Derived<int>::mem_f()\n"; };
  22. };
  23.  
  24. Base* menu()
  25. {
  26. int x; std::cin >> x;
  27. if (x == 42) {
  28. return new Derived<std::string>;
  29. }
  30. return new Derived<int>;
  31. }
  32. int main()
  33. {
  34. Base *base1 = menu();
  35. base1->mem_f();
  36.  
  37. Base *base2 = menu();
  38. base2->mem_f();
  39.  
  40. delete base1;
  41. delete base2;
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0s 3432KB
stdin
42
5
stdout
Derived<std::string>::mem_f()
Derived<int>::mem_f()