+ 2
When does vtable and vpointer linking happens in case of virtual polymorphism
Refer code below and would like to know why it happens like this ? I expected to get test method called from base constructor : https://code.sololearn.com/ce9868qk5aQE/?ref=app
4 odpowiedzi
+ 1
Matthias this is perfectly fine... but could you please link it with mechanism of polymorphism now and still feel whatever you told seems okay to you?
afaik, once you compile code, vtable is created and hence in vtable ; due to virtual keyword, test from child class is associated due to derived class object
+ 1
then why method of base class gets called if objeCt is pointing to child class method
0
Why do you think it is weird?
At the point of new derived, first the base class constructor is called and since there is no other test function, it prints the base.
Then the constructor of derived is called and uses the test function of derived class.
Now there is a pointer of type base, pointing to the newly created object of derived class.
Since the test function is virtual, vtable kicks in and calls the test function of derived class.
0
Well, yes... That's exactly what happens here.
I don't really understand your problem.