When does vtable and vpointer linking happens in case of virtual polymorphism | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

16th Sep 2018, 8:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 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
16th Sep 2018, 10:28 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
then why method of base class gets called if objeCt is pointing to child class method
16th Sep 2018, 12:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
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.
16th Sep 2018, 9:19 AM
Matthias
Matthias - avatar
0
Well, yes... That's exactly what happens here. I don't really understand your problem.
16th Sep 2018, 12:11 PM
Matthias
Matthias - avatar