Vtables themselves are generally stored in the static data segment, as they are class-specific (vs. object-specific).
For every class that contains virtual functions, the compiler constructs a virtual table, a.k.a vtable. The vtable contains an entry for each virtual function accessible by the class and stores a pointer to its definition. Only the most specific function definition callable by the class is stored in the vtable.
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.
Basically, 2. One for class A , one for class B (vftables) and 2 vfptrs, one for a1 and one for b1 . However, this is not standard mandated, so you could as well have none. (usually implementations use vftables, but its not mandated.
how are virtual tables stored in memory? their layout?
e.g.
class A{
public:
virtual void doSomeWork();
};
class B : public A{
public:
virtual void doSomeWork();
};
How will be the layout of virtual tables of class A and class B in memory?
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