c++ - Overriding template methods -


i'm trying override template method. here minimal example:

#include <iostream>  class base { public:     template <typename t>     void print(t var)     {         std::cout << "this generic place holder!\n";     } };  class a: public base { public:     template <typename t>     void print(t var)     {         std::cout << "this printing " << var << "\n";     } };  class b: public base { public:     template <typename t>     void print(t var)     {         std::cout << "this b printing " << var << "\n";     } };   int main() {     base * base[2];     base[1] = new a;     base[2] = new b;      base[1]->print(5);     base[2]->print(5);      delete base[1];     delete base[2];     return 0; } 

the output in both cases this generic place holder! how can achieve methods of derived classes called?

if method not template, define virtual , work expected (already tried that), needs template. doing wrong?

first member function template cannot virtual, , member function template in derived class cannot override virtual member function base class.

as code, type of pointer base*, in template member function instantiation, function in base instantiated, that's why function base called.

if use a* , b*, functions in children instantiated , called.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -