vtable
A vtable is the per-type table of function pointers the compiler builds for a class that has virtual functions. Each object of that class stores a hidden pointer (the vptr) to the table. A virtual call loads the slot and jumps.
Detailed Explanation
The C++ standard never says "vtable". Every mainstream ABI implements virtual dispatch that way, and you need the picture to understand object size, dynamic_cast, and why a missing virtual destructor leaks.
When the compiler sees a class with at least one virtual function, it emits a static table for that class: slot 0 might be ~T, slot 1 draw, slot 2 area. A Circle that overrides draw gets its own table: slot 0 ~Circle, slot 1 Circle::draw, slot 2 Shape::area if area was not overridden. The tables live in read-only data. There is one table per type, not per object.
Each object starts (on the Itanium ABI, for a single-inheritance primary base) with a vptr that the constructor sets to that type's table. Construction is staged: Shape's constructor sets the vptr to Shape's table, then Circle's constructor resets it to Circle's table. That is why a virtual call from Shape::Shape cannot reach Circle::draw. Destruction reverses the process.
A virtual call p->draw() becomes: load the vptr from p, load the draw slot, call that address, passing p as this. If p actually points at a Circle, the slot is Circle::draw. The static type of p only decides which slot index to use; it does not decide the function.
Multiple inheritance adds more tables. A Button that inherits Drawable and Clickable has a vptr for each polymorphic base. A Clickable* pointing at a Button may not equal the Button*; the compiler adjusts this when it calls through the secondary vtable. dynamic_cast walks type-info structures hung off those tables (RTTI) to decide whether an adjustment is possible.
Cost you can measure:
- Size. One pointer per polymorphic object (8 bytes on 64-bit), plus more if there are multiple polymorphic bases. A class you thought was 4 bytes is 16 after you add a virtual function (vptr plus padding plus the int).
- Call. An extra load and an indirect jump. The CPU's indirect-branch predictor usually handles a stable set of types. A megamorphic call site (dozens of derived types) predicts poorly.
- Inlining. The compiler cannot inline a virtual call unless it can prove the dynamic type (devirtualisation).
finaland linking-time optimisation help that proof.
You cannot portably read the vptr. You should not write one. What you can do is sizeof, and notice that adding the first virtual function grew the object. You can also dynamic_cast and typeid, which depend on the RTTI sitting next to the vtable.
If a type does not need runtime polymorphism, do not add a virtual destructor "for safety" unless you actually delete through a base. A value type with no virtuals stays a value type: no vptr, trivial copies if the members allow it, and it can live in a packed array.
Code Examples
#include <iostream>
struct Empty { int n; };
struct Poly {
virtual ~Poly() = default;
int n;
};
int main() {
std::cout << sizeof(Empty) << '\n'; // typically 4
std::cout << sizeof(Poly) << '\n'; // typically 16: vptr + int + padding
}#include <iostream>
struct Base {
virtual const char* name() const { return "base"; }
virtual ~Base() = default;
};
struct Derived : Base {
const char* name() const override { return "derived"; }
};
void speak(const Base& b) {
std::cout << b.name() << '\n'; // indirect call through b's vptr
}
int main() {
Derived d;
speak(d); // prints: derived
}Common Gotchas
The first virtual function changes layout
adding `virtual ~T()` to a class used in a C API or a shared-memory struct breaks binary compatibility. Keep wire types free of virtuals.
There is no standard layout for the vptr
Itanium puts it at offset 0 of the primary base. MSVC has its own rules. Do not serialise objects by dumping bytes if they have a vptr.
Slicing drops the vptr of the derived type
`Base b = derived;` constructs a `Base` and copies only the `Base` part. Virtual calls on `b` hit `Base`. Store pointers or references when you need polymorphism.
Calling a virtual through a destroyed object
after `~Derived` starts, the vptr may already point at `Base`. Do not store `this` in a global during destruction and call virtuals later.