The Secret Life of C++: Virtual Inheritance

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. TODO: Slicing them off for passing references to base class.

        

        

        

        

        

        

        

        

        

        

Polymorphic Function Pointers


        

        

        

        

        

More information, including a better description of what VTT is for, at http://stackoverflow.com/questions/6258559/what-is-the-vtt-for-a-class.

TODO: Build an example that needs VTT.
TODO: Draw some pictures.

Fork me on GitHub