The Secret Life of C++: Day 3: Virtual Inheritance Revisitted

Last time we met, we didn't make it all the way through a discussion of virtual inheritance. Lets come back to that.

Dreaded Diamond Problem

This is when we have a base class, a pair of derived classes, and we want a class that inherits from both derrived classes.

Virtual Base Classes in Memory

Classes with a virtual base will have an entry in their VTable indicating the offset at which that class can be found. Virtual functions accessing the virtual base class will use this offset.

There will also be trampoline functions for any virtual function that is not overridden, which sets the this* appropriately using that offset.

An example of a simple virtual base class: virtual-sub.cc, virtual-sub.s, virtual-sub.listing.

An example of the diamond problem: virtual-diamond.cc, virtual-diamond.s, virtual-diamond.listing.

Virtual Base Classes in the VTable

There are two things we will find the VTable: Entries for the offsets, and

How they are constructed

When constructing a class with Virtual base classes, we need to populate all the various VTable entries.
Richard Tibbetts
Last modified: Wed Jan 21 17:36:31 EST 2004