fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class Base { // это должен быть абстрактный класс
  5. public:
  6. // Base();
  7. virtual int foo(T x) = 0; // - пробовал вот так не получается
  8. };
  9.  
  10. class A : public Base<int> {
  11. public:
  12. int foo(int x) {
  13. std::cout << "A::foo()\n";
  14. return 1;
  15. }
  16. };
  17.  
  18. class B : public Base<float> {
  19. public:
  20. int foo(float x) {
  21. std::cout << "B::foo()\n";
  22. return 2;
  23. }
  24. };
  25.  
  26. template <typename T>
  27. int func(Base<T> *a, T value) {
  28. return a->foo(value); // <- вот тут надо вызвать
  29. }
  30.  
  31. int main() {
  32. func(new A(), 10);
  33. func(new B(), 10.1f);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
A::foo()
B::foo()