Reputation: 665
For those compiler implementations that use vtables: are there any cases when virtual functions tables are changed at run time? Or are vtables only filled at compile time, and no actions are performed to modify them at run time?
Upvotes: 13
Views: 2872
Reputation: 69912
The short answer is no.
A slightly longer (and probably implementation specific) answer is that the object's pointer to the actual vtable changes during the execution of a constructor and destructor of a derived polymorphic class, so that overridden methods in a derived class do not get executed by the base class's constructor/destructor while the derived class is not yet constructed/has been destructed.
If you want objects to change class during run time then you have a number of options:
objective-c(++)
hand-code your own dispatch mechanism
python/javascript etc al.
(the best option) reconsider your design.
Upvotes: 3
Reputation: 385305
I am not aware of any C++ ABI with a polymorphism implementation that employs virtual tables changing at runtime.
It wouldn't be very useful, anyway, since virtual tables typically describe a property of the code (the relationship of member functions to each other w.r.t. position in class hierarchy) and C++ code doesn't change at runtime.
And because it wouldn't be useful, it would be wasteful.
Upvotes: 8