I've written code in cpp to test my understanding of dynamic dispatching. I think that in my program the output should be "I'm in NT". My reasoning is:
- tMethod is defined to be virtual, so dynamic binding will be used
- at run time the class-type of
testis NT, so call totest->tMethod(ont)should look for the implementation inside NT - actual parameter
ontis of type NT, so exact match is found to be NT's impementation of tMethod
However, the output of this program is "I'm in T".
What is wrong in my reasoning?
#include <iostream>
using namespace std;
class T {
public:
virtual void tMethod(T){
cout<<"I'm in T"<<endl;
}
};
class NT: public T{
public:
void tMethod(NT){
cout<<"I'm in NT"<<endl;
}
};
int main()
{
NT ont;
T* test=new NT();
test->tMethod(ont);
return 0;
}