2.5 Virtual Table Layout

2.5.1 General

A virtual table (vtable) is a table of information used to dispatch virtual functions, to access virtual base class subobjects, and to access information for runtime type identification (RTTI). Each class that has virtual member functions or virtual bases has an associated set of virtual tables. There may be multiple virtual tables for a particular class, if it is used as a base class for other classes. However, the virtual table pointers within all the objects (instances) of a particular most-derived class point to the same set of virtual tables.

A virtual table consists of a sequence of offsets, data pointers, and function pointers, as well as structures composed of such items. We will describe below the sequence of such items. Their offsets within the virtual table are determined by that allocation sequence and the natural ABI size and alignment, just as a data struct would be. In particular:

Offsets are of type ptrdiff_t unless otherwise stated.

unless otherwise stated. Data pointers have normal pointer size and alignment.

On Itanium, function pointers are pairs: the function address follwed by the global pointer value that should be used when calling the function, aligned as for a pointer. On other platforms, function pointers are represented as they would be in any other context.

In general, what we consider the address of a virtual table (i.e. the address contained in objects pointing to a virtual table) may not be the beginning of the virtual table. We call it the address point of the virtual table. The virtual table may therefore contain components at either positive or negative offsets from its address point.

2.5.2 Virtual Table Components and Order

This section describes the usage and relative order of various components that may appear in virtual tables. Precisely which components are present in various possible virtual tables is specified in the next section. If present, components are present in the order described, except for the exceptions specified.

Virtual call (vcall) offsets are used to perform pointer adjustment for virtual functions that are declared in a virtual base class or its subobjects and overridden in a class derived from it. These entries are allocated in the virtual table for the virtual base class that is most immediately derived from the base class containing the overridden virtual function declaration. They are used to find the necessary adjustment from the virtual base to the derived class containing the overrider, if any. When a virtual function is invoked via a virtual base, but has been overridden in a derived class, the overriding function first adds a fixed offset to adjust the this pointer to the virtual base, and then adds the value contained at the vcall offset in the virtual base to its this pointer to get the address of the derived object where the function was overridden. These values may be positive or negative. These are first in the virtual table if present, ordered as specified in categories 3 and 4 of Section 2.5.3 below.

are used to perform pointer adjustment for virtual functions that are declared in a virtual base class or its subobjects and overridden in a class derived from it. These entries are allocated in the virtual table for the virtual base class that is most immediately derived from the base class containing the overridden virtual function declaration. They are used to find the necessary adjustment from the virtual base to the derived class containing the overrider, if any. When a virtual function is invoked via a virtual base, but has been overridden in a derived class, the overriding function first adds a fixed offset to adjust the pointer to the virtual base, and then adds the value contained at the vcall offset in the virtual base to its pointer to get the address of the derived object where the function was overridden. These values may be positive or negative. These are first in the virtual table if present, ordered as specified in categories 3 and 4 of Section 2.5.3 below. Virtual Base (vbase) offsets are used to access the virtual bases of an object. Such an entry is added to the derived class object address (i.e. the address of its virtual table pointer) to get the address of a virtual base class subobject. Such an entry is required for each virtual base class. The values can be positive or negative.

However, in classes sharing a virtual table with a primary base class, the vcall and vbase offsets added by the derived class all come before the vcall and vbase offsets required by the base class, so that the latter may be laid out as required by the base class without regard to additions from the derived class(es). The offset to top holds the displacement to the top of the object from the location within the object of the virtual table pointer that addresses this virtual table, as a ptrdiff_t . It is always present. The offset provides a way to find the top of the object from any base subobject with a virtual table pointer. This is necessary for dynamic_cast<void*> in particular.

In a complete object virtual table, and therefore in all of its primary base virtual tables, the value of this offset will be zero. For the secondary virtual tables of other non-virtual bases, and of many virtual bases, it will be negative. Only in some construction virtual tables will some virtual base virtual tables have positive offsets, due to a different ordering of the virtual bases in the full object than in the subobject's standalone layout. The typeinfo pointer points to the typeinfo object used for RTTI. It is always present. All entries in each of the virtual tables for a given class must point to the same typeinfo object. A correct implementation of typeinfo equality is to check pointer equality, except for pointers (directly or indirectly) to incomplete types. The typeinfo pointer is a valid pointer for polymorphic classes, i.e. those with virtual functions, and is zero for non-polymorphic classes. The virtual table address point points here, i.e. this is the virtual table address contained in an object's virtual pointer. This address must have the alignment required for pointers. Virtual function pointers are used for virtual function dispatch. Each pointer holds either the address of a virtual function of the class, or the address of a secondary entry point that performs certain adjustments before transferring control to a virtual function. The form of a virtual function pointer is specified by the processor-specific C++ ABI for the implementation. In the specific case of 64-bit Itanium shared library builds, a virtual function pointer entry contains a pair of components (each 64 bits): the value of the target GP value and the actual function address. That is, rather than being a normal function pointer, which points to such a two-component descriptor, a virtual function pointer entry is the descriptor. The order of the virtual function pointers in a virtual table is the order of declaration of the corresponding member functions in the class. There is an entry for any virtual function declared in a class, whether it is a new function or overrides a base class function, unless it overrides a function from the primary base, and conversion between their return types does not require an adjustment. (In the case of this exception, the primary base and the derived class share the virtual table, and can share the virtual function entry because their 'this' and result type adjustments are the same.) If a class has an implicitly-defined virtual destructor, its entries come after the declared virtual function pointers. When a derived class and its primary base share a virtual table, the virtual function entries introduced by the derived class follow those for the primary base, so that the layout of the primary base's embedded virtual table is the same as that of its standalone virtual table. In particular, if the derived class overrides a base class virtual function with a different (covariant) return type, the entry for the derived class comes after the primary base's embedded virtual table in declaration order, and is the entry used for calls from the derived class without adjustment. The entry in the embedded primary virtual table points to a routine that adjusts the result pointer before returning. The entries for virtual destructors are actually pairs of entries. The first destructor, called the complete object destructor, performs the destruction without calling delete() on the object. The second destructor, called the deleting destructor, calls delete() after destroying the object. Both destroy any virtual bases; a separate, non-virtual function, called the base object destructor, performs destruction of the object but not its virtual base subobjects, and does not call delete().

are used to access the virtual bases of an object. Such an entry is added to the derived class object address (i.e. the address of its virtual table pointer) to get the address of a virtual base class subobject. Such an entry is required for each virtual base class. The values can be positive or negative.

Following the primary virtual table of a derived class are secondary virtual tables for each of its proper base classes, except any primary base(s) with which it shares its primary virtual table. These are copies of the virtual tables for the respective base classes (copies in the sense that they have the same layout, though the fields may have different values). We call the collection consisting of a primary virtual table along with all of its secondary virtual tables a virtual table group. The order in which they occur is the same as the order in which the base class subobjects are considered for allocation in the derived object:

First are the virtual tables of direct non-primary, non-virtual proper bases, in the order declared, including their secondary virtual tables for non-virtual bases in the order they appear in the standalone virtual table group for the base. (Thus the effect is that these virtual tables occur in inheritance graph order, excluding primary bases and virtual bases.)

Then come the virtual base virtual tables, also in inheritance graph order, and again excluding primary bases (which share virtual tables with the classes for which they are primary).

2.5.3 Virtual Table Construction

In this section, we describe how to construct the virtual table for an class, given virtual tables for all of its proper base classes. To do so, we divide classes into several categories, based on their base class structure.

Category 0: Trivial

No virtual base classes.

No virtual functions.

Such a class has no associated virtual table, and an object of such a class contains no virtual pointer.

Category 1: Leaf

No inherited virtual functions.

No virtual base classes.

Declares virtual functions.

The virtual table contains offset-to-top and RTTI fields followed by virtual function pointers. There is one function pointer entry for each virtual function declared in the class, in declaration order, with any implicitly-defined virtual destructor pair last.

Category 2: Non-Virtual Bases Only

Only non-virtual proper base classes.

Inherits virtual functions.

The class has a virtual table for each proper base class that has a virtual table. The secondary virtual table for a base class B has the same contents as the primary virtual table for B, except that:

The offset-to-top and RTTI fields contain information for the class, rather than for the base class.

The function pointer entries for virtual functions inherited from the base class and overridden by this class are replaced with the addresses of the overriding functions (or the corresponding adjustor secondary entry points).

For a proper base class Base , and a derived class Derived for which we are constructing this set of virtual tables, we shall refer to the virtual table for Base as Base-in-Derived . The virtual pointer of each base subobject of an object of the derived class will point to the corresponding base virtual table in this set.

The primary virtual table for the derived class contains entries for each of the functions in the primary base class virtual table, replaced by new overriding functions as appropriate. Following these entries, there is an entry for each virtual function declared in the derived class (in declaration order) for which one of the following two conditions holds:

The virtual function does not override any function already appearing in the virtual table.

The virtual function overrides a function (or functions) appearing in the virtual table, but the return type of the overrider is substantively different from the return type of the function(s) already present. If the return types are different, they are both pointer-to-class types, or both reference-to-class types. Let B and D denote the classes, where D is derived from B. The types are substantively different if B is a morally virtual base of D or if B is not located at offset zero in D.

The primary virtual table can be viewed as two virtual tables accessed from a shared virtual table pointer.

A benefit of replicated virtual function entries (i.e., entries that appear both in the primary virtual table and in a secondary virtual table) is that they reduce the number of this pointer adjustments during virtual calls. Without replication, there would be more cases where the this pointer would have to be adjusted to access a secondary virtual table prior to the call. These additional cases would be exactly those where the function is overridden in the derived class, implying an additional thunk adjustment back to the original pointer. Replication saves two 'this' adjustments for each virtual call to an overridden function originally introduced by a non-primary proper base class.

Category 3: Virtual Bases Only

Structure:

Only virtual base classes (but those may have non-virtual bases).

The virtual base classes are neither empty nor nearly empty.

The class has a virtual table for each virtual base class that has a virtual table. These are all secondary virtual tables, because there are no empty or nearly empty base classes to be primary, and they are constructed from copies of the base class full object virtual tables according to the same rules as in Category 2, except that the virtual table for a virtual base A also includes a vcall offset entry for each virtual function represented in A's primary virtual table and the secondary virtual tables from A's non-virtual bases.

The vcall offsets in the secondary virtual table for a virtual base A are ordered as described next. We describe the ordering from the entry closest to the virtual table address point to that furthest. Since the vcall offsets precede the virtual table address point, this means that the memory address order is the reverse of that described.

If virtual base A has a primary base class P sharing its virtual table, P's vcall offsets come first, in the same order they would appear if P itself were the virtual base.

Next come vcall offsets for each virtual function declared in A, in declaration order. Note that even for an overriding virtual function with covariant return types, only one vcall offset is present, as it can be shared by both virtual table entries.

Finally come vcall offsets for virtual functions declared in non-virtual bases of A other than P. These bases are considered in inheritance graph preorder, and the vcall offsets for multiple functions declared in one of them are in declaration order.

If the above listing of vcall offsets includes more than one for a particular virtual function signature, only the first one (closest to the virtual table address point) is allocated. That is, an offset from primary base P (and its non-virtual bases) eliminates any from A or its other bases, an offset from A eliminates any from the non-primary bases, and an offset from a non-primary base B of A eliminates any from the bases of B.

Note that there are no vcall offsets for virtual functions declared in a virtual base class V of A and never overridden within A or its non-virtual bases. Calls to such functions will use the vcall offset in V's virtual table.

The class also has a virtual table that is not copied from the virtual base class virtual tables. This virtual table is the primary virtual table of the class and is addressed by the virtual table pointer at the top of the object, which is not shared because there are no nearly empty virtual bases to be primary. It holds the following function pointer entries, following those of any primary base's virtual table, in the virtual functions' declaration order:

Entries for virtual functions introduced by this class, i.e. those not declared by any of its bases.

Entries for overridden virtual functions from the base classes, called replicated entries because they are already in the secondary virtual tables of the class.

The primary virtual table also has virtual base offset entries to allow finding the virtual base subobjects. There is one virtual base offset entry for each virtual base class, direct or indirect. The entries are in the reverse of the inheritance graph order. That is, the entry for the leftmost virtual base is closest to the address point of the virtual table.

Category 4: Complex

Structure:

None of the above, i.e. directly or indirectly inherits both virtual and non-virtual proper base classes, or at least one nearly empty virtual base class.

The rules for constructing virtual tables of the class are a combination of the rules from Categories 2 and 3, and can generally be determined inductively. The differences are mostly due to the fact that virtual base classes can now have (nearly empty) primary bases:

If virtual base A has a primary virtual base class P sharing its virtual table, P's vbase and vcall offsets come first in the primary virtual table, in the same order they would appear if P itself were the virtual base, and those from A that do not replicate those from P precede them.

As for the non-virtual base case, virtual function pointer entries from the derived class introductions occur only after the entries from the primary base class. There are entries for overridden virtual functions from the primary base class only if the result types are different (covariant). For purposes of this case, the two types are considered different if one of them is a non-primary or virtual base class of the other.

For an S-as-T virtual table, the vbase offset entries from the primary virtual table for T are replaced with appropriate offsets given the completed hierarchy.

Consider the following inheritance hierarchy: struct S { virtual void f() }; struct T : virtual public S {}; struct U : virtual public T {}; struct V : public T, virtual public U {};

T's virtual table contains a virtual base offset for S. U's virtual table contains virtual base offsets for S and T. V's virtual table contains virtual base offsets for S, U, and T (in reverse inheritance graph preorder), where the vbase offset for T is for the virtual base of U, not for the non-virtual direct base of V.

Consider in addition: struct W : public T {};

T is a primary base class for W. Therefore, its virtual base offset for S in its embedded T-in-W virtual table is the only one present.

The above-described virtual table group layout would allow all non-virtual secondary base class virtual tables in a group to be accessed from a virtual pointer for one of them, since the relative offsets would be fixed. (Since the primary virtual table could end up being embedded, as the primary base class virtual table, in another virtual table with additional virtual pointers separating it from its secondary virtual tables, this observation is not true of the primary virtual table.) However, since construction virtual table groups may be organized differently (see below), an implementation may not depend on this relationship between secondary virtual tables. This tradeoff was made because the space savings resulting from not requiring construction virtual tables to occur in complete groups was considered more important than potential sharing of virtual pointers.