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?
Virtual tables are created during compilation but used at runtime. Show activity on this post. As you say, the virtual function table (and other polymorphic information) for each class is generated at compile time. Each object contains a pointer to the correct table for its dynamic type.
You can imagine what happens when you perform inheritance and override some of the virtual functions. The compiler creates a new VTABLE for your new class, and it inserts your new function addresses using the base-class function addresses for any virtual functions you don't override.
Vtable is like an array of function pointer. Vtable and Vptr is creating at compile time which will get memory in run time and vtable entries are virtual function addresses . Every object of a class containing a virtual function will have an extra pointer which is pointing to Virtual Table is known as virtual pointer.
vTable : Every class that contains a virtual function or more has a vTable assigned to it. vTable is a different kind of function pointer array that has the address of all virtual functions of the class. These are built by the compiler during compile time.
This process is known as static dispatch or early binding: the compiler knows which routine to execute during compilation. So, what do vtables have to do with all this? Well, there are cases where it is not possible for the compiler to know which routine to execute at compile time. This is the case, for instance, when we declare virtual functions:
5. vtable is created at compile time and is used at run time. 6. vtable will be having function pointer entry to all the virtual functions present and derived in the class. 7. vtable will store NULL value for pure virtual function. 1. vptr is also known as vtable pointer. This pointer will point to vtable.
These are built by the compiler during compile time. vPointer : vPointers are associated with every objects of a class containing a vTable. They are stored in the first 4 bytes of memory. vPointers are used to find the function address during the runtime to do the binding as the virtual functions linking are not done at compile time.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With