Template Child Class Overriding a Parent Class's Virtual Function
- by user334066
The below code compiles with gcc v4.3.3 and the templated child class seems to be overriding a virtual function in the parent, but doesn't that break the rule that you cannot have a virtual template function? Or is something else happening that I don't understand?
class BaseClass
{
public:
virtual void Func(int var)
{
std::cout<<"Base int "<<var<<std::endl;
}
virtual void Func(double var)
{
std::cout<<"Base double "<<var<<std::endl;
}
};
template <class TT>
class TemplateClass : public BaseClass
{
public:
using BaseClass::Func;
virtual void Func(TT var)
{
std::cout<<"Child TT "<<var<<std::endl;
}
};
int main(int argc, char **argv)
{
BaseClass a;
TemplateClass<int> b;
BaseClass *c = new TemplateClass<int>;
int intVar = 3;
double doubleVar = 5.5;
a.Func(intVar);
a.Func(doubleVar);
b.Func(intVar);
b.Func(doubleVar);
c->Func(intVar);
c->Func(doubleVar);
delete c;
}
This then outputs:
Base int 3
Base double 5.5
Child TT 3
Base double 5.5
Child TT 3
Base double 5.5
as I hoped, but I'm not sure why it works.