Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ?
The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations.
It has no type. It's an implementation detail unspecified by the standard; it is not part of the language.
The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (abbreviated as VPTR), which points to the VTABLE for that object.
Vptr and Vtable get stored in Data Segment... 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 .
The runtime class of the object is a property of the object itself. In effect, vptr
represents the runtime class, and therefore can't be static
. What it points to, however, can be shared by all instances of the same runtime class.
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