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

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -