joe wrote:
I expected this to work:
class Base
{
virtual void go(int *) {cout<<"BASE CLASS"<<endl;}
};
class Derived : public Base
{
template<typename T>
void go(T*) {cout<<"Derived Class"<<endl;}
};
int main(){
Base *b = new Derived;
b->go(new int[30]);
}
I get "BASE CLASS". I expected the virtual function to work and find
the derived class function.
What's the nitty gritty here of what's going on?
Name resolution is performed statically (during compile time).
Derived::go does not override Base::go, it hides it (actually in this
case you might get a compilation error if it is ever instantiated for
int*). However, since you are calling through a pointer to Base,
Base::go is in scope and is resolved. As there is no Derived::go that
overrides Base::go the pointer in the table is to Derived::go and that
is what is called. No instance of the Derived::go template function is
created as it is never called; such instantiation can only occur
statically and cannot be virtual.