Layout of class memory under multiple or virtual inheritance and vtable (s)?

I am reading "Inside the C ++ Object Model", trying to understand how multiple and virtual inheritance is achieved through vtables. (I understand that one polymorphism is excellent, good).

I am having difficulty understanding what exactly is done when the method should be located during virtual inheritance or during casting, because there are many offset calculations that need to be performed.

Will someone help with understanding how multiple vtables are used in the example with multiple or virtual inheritance? If I could understand the layout and the problem, I probably could better understand this problem.

+8
c ++ inheritance polymorphism multiple-inheritance virtual-inheritance
source share
1 answer

C ++ implementations typically use vtables to implement virtual functions. The vtable is a table of function pointers. Each object of a class with virtual functions has a hidden pointer to a vtable containing the addresses of all virtual functions of the class.

When a virtual function is called, the code calculates the offset of the function pointer in the vtable and calls the function whose address is stored there.

enter image description here

When a derived class of a base class overrides the virtuall function, the virtual table of that class simply points to an overridden function instead of the original one.

This excellent article explains in detail how it works for both single and multiple inheritance.

+10
source share

All Articles