c++ - Overriding a method returning typename T does not work -
#include <iostream> #include <string> using namespace std; template <typename t> class { public: a() { overrideme(); } virtual ~a(){}; virtual t overrideme() { throw string("a.overrideme called"); } protected: t member; }; class b : public a<double> { public: b(){}; virtual ~b(){}; virtual double overrideme() { throw string("b.overrideme called"); } }; int main() { try { b b; } catch(string s) { cout << s << endl; //this prints: a.overrideme called } return 0; }
you can override method template base class, can shown in example:
#include <iostream> template <typename t> struct foo { virtual t foo() const { std::cout << "foo::foo()\n"; return t(); } }; struct bar : foo<double> { virtual double foo() const { std::cout << "bar::foo()\n"; return 3.14; } }; int main(){ bar b; double x = b.foo(); } output:
bar::foo()
Comments
Post a Comment