Next: , Previous: Inheritance, Up: Cplusplus


8.13 Virtual Base Classes

A derived class object consists of a concatenation in memory of the data areas defined by each base class, starting with the leftmost and ending with the rightmost in the list of base classes. The exception to this rule is for virtual inheritance. In the example above, class D inherits virtually from base class B. This means that an instance of a D object will not contain its own B part but merely a pointer to a B part, known as a virtual base pointer.

In a derived class stab, the base offset part of the derivation information, described above, shows how the base class parts are ordered. The base offset for a virtual base class is always given as 0. Notice that the base offset for B is given as 0 even though B is not the first base class. The first base class A starts at offset 0.

The field information part of the stab for class D describes the field which is the pointer to the virtual base class B. The vbase pointer name is ‘$vb’ followed by a type reference to the virtual base class. Since the type id for B in this example is 25, the vbase pointer name is ‘$vb25’.

     .stabs "D:Tt31=s32!3,000,20;100,25;0264,28;$vb25:24,128;Ddat:1,
            160,32;A_virt::32=##1;:i;2A*-2147483647;20;;B_virt::32:i;
            2A*-2147483647;25;;C_virt::32:i;2A*-2147483647;28;;D_virt:
            :32:i;2A*-2147483646;31;;;~%20;",128,0,0,0

Following the name and a semicolon is a type reference describing the type of the virtual base class pointer, in this case 24. Type 24 was defined earlier as the type of the B class this pointer. The this pointer for a class is a pointer to the class type.

     .stabs "this:P24=*25=xsB:",64,0,0,8

Finally the field offset part of the vbase pointer field description shows that the vbase pointer is the first field in the D object, before any data fields defined by the class. The layout of a D class object is a follows, Adat at 0, the vtable pointer for A at 32, Cdat at 64, the vtable pointer for C at 96, the virtual base pointer for B at 128, and Ddat at 160.