Dynamic dispatching OOP

Viewed 105

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:

  1. tMethod is defined to be virtual, so dynamic binding will be used
  2. at run time the class-type of test is NT, so call to test->tMethod(ont) should look for the implementation inside NT
  3. actual parameter ont is 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;
    }
3 Answers
Related