The idea: an object is its base parts plus its own fields
A derived object contains a complete object of each base class, called a base subobject. The compiler lays the base subobjects out first, in the order the bases are listed, and puts the derived class's own fields after them. A pointer to a base must point at that base's subobject. So when a base does not sit at offset 0, converting the pointer changes its value.
How to read the page
- C++ source: the classes, then the statements the current action runs.
- Memory: the object at 0x1000, 8 bytes per row, each row two 4-byte cells. A vptr fills a row. The colour says which subobject a cell belongs to. Pointer boxes on the left point at the address they hold.
- vtables: one table per subobject that has a vptr, named T1, T2, T3. The vptr cells say which one they point to.
- The layout is the Itanium C++ ABI that GCC and Clang use on 64-bit Linux and
macOS (x86-64, arm64): a pointer is 8 bytes, an
int4. Every offset and size was checked with Clang's-fdump-record-layouts. The addresses are made up.
One base: the base part comes first
struct S : A { int s; } starts with a whole A: its vptr at 0 and
a at 8. s comes after. A pointer to the S is already a
pointer to its A, so A* pa = ps adds 0. That is the case
C++ virtual functions shows in detail.
(Demo: single inheritance)
One surprise: sizeof(S) is 16, the same as sizeof(A). An A alone
ends with 4 bytes of tail padding (see Struct padding).
The Itanium ABI places a derived class's fields at the base's data size (12), not at its
sizeof (16), so s reuses that padding. It only does this for bases that are not plain
C-style structs.
Two bases: two vptrs and a pointer that moves
struct C : A, B holds an A part at offset 0 and a B part at
offset 16, then c at 28. Each part keeps its own vptr, so a C has two.
Code that takes a B* expects a B at that address: a vptr, then
b 8 bytes later. So B* pb = pc compiles to pb = pc + 16.
(Demo: upcast to the second base adds 16)
| Statement | Value | Adjustment |
|---|---|---|
C* pc = &c; | 0x1000 | |
A* pa = pc; | 0x1000 | +0 |
B* pb = pc; | 0x1010 | +16 |
C* back = static_cast<C*>(pb); | 0x1000 | −16 |
A null pointer must stay null, so the compiler adds a check: pb = pc ? pc + 16 : nullptr.
This is also why casting a C* through void* to a B* is a bug:
the +16 is skipped.
Calls through the second base: thunks
C::fb is compiled for this = the start of the C. A call
pb->fb() reads the vptr at 0x1010, which leads to T2, the table for C's B part. T2's
slot holds a small thunk: subtract 16 from this, then jump to
C::fb. T2 also stores offset_to_top = −16, which
dynamic_cast<void*> uses to find the whole object. A call through
pa needs no thunk, because the A part is at offset 0.
(Demo: call through B*)
The diamond: two copies of Base
If L and R both derive from Base and D derives from
both, the L part and the R part each hold a Base. D is 20 bytes with two
x fields, at 0 and 8. d.x and Base* b = &d do not compile:
they are ambiguous. You have to name a path: d.L::x, or go through an
L* or R*, and the two paths reach two different objects.
(Demo: diamond)
Virtual inheritance: one shared Base
With struct VL : virtual Base and struct VR : virtual Base, the most derived
object holds one Base. The Itanium ABI puts it after all the other
parts: in VD at offset 32, so VD is 40 bytes. Each part that inherits
virtually gets a vptr, even without virtual functions.
(Demo: virtual inheritance)
The offset of the shared Base is not fixed. In a plain VL object (16 bytes) it
sits at 12; inside a VD it sits 32 bytes from the VL part. So code holding a
VL* reads it at run time: it loads the vptr and the vbase_offset
stored in front of the table. T1 says 32, T2 (the VR part, at 0x1010) says 16, and both paths reach
0x1020. (Demo: reaching the virtual base)
| D : L, R | VD : VL, VR (virtual Base) | |
|---|---|---|
| sizeof | 20 | 40 |
| Base subobjects | 2 | 1 |
| vptrs | 0 | 2 |
| Reaching Base | fixed offset, chosen path | vptr, then vbase_offset |
Slicing: passing a derived object by value
void show(A a) takes an A by value. show(c) builds a new
16-byte A for the parameter and copies only the A part into it. The parameter's vptr is
not copied: A's constructor points it at A's own table. So
a.fa() runs A::fa, and the B part and c (16 of 32 bytes) are
gone. This is object slicing. Take const A& or A* to
keep the whole object. (Demo: pass by value)
See also C++ virtual functions for one vptr and the dispatch hop by hop, Struct padding and alignment for the padding rules, Overloading vs overriding (in Java) for which method a call picks, The rule of three for copy constructors, and Inheritance vs composition for when to inherit at all.
What the page leaves out
The vtables drawn here skip the RTTI pointer (type information) that sits next to
offset_to_top, virtual destructors (two slots each), the VTT and construction
vtables used while a class with virtual bases is being built, virtual-base thunks that read an
offset at run time, covariant return types and empty-base optimisation. MSVC uses a different
layout (a separate virtual base table pointer). The C++ standard does not fix any of this; it is the
ABI's choice.