Interview Q&A · C++ · 8 questions in this topic

Virtual Dispatch & vtables — C++ Interview Q&A

All C++ topics

How runtime polymorphism is implemented — vtables, vptr, dispatch cost, and when to avoid virtuals. Part of Inheritance & Polymorphism · 213 questions in the C++ bank.

How does virtual dispatch work (vptr and vtable)?

Answer

Each polymorphic class gets a static vtable (array of function pointers, one per virtual). Each object carries a hidden vptr to its class vtable. A virtual call loads the function address through the vptr and jumps — so the dynamic type, not the static type, selects the implementation.

Construction sets the vptr progressively (which is why ctor virtual calls do not reach derived overrides), and the first virtual call pays an extra indirection plus lost inlining.

Key points

  1. 1vtable — one per class: addresses of that class virtuals.
  2. 2vptr — one per object: points at its dynamic-class vtable.
  3. 3call — load through vptr, indirect jump; hence the small cost.

Example

Example.cppcpp
Base* p = new Derived();
p->f(); // loads Derived vtable entry for f through p vptr

💡 Draw the triangle in words — object holds vptr, vptr points at class vtable, vtable points at functions.

What is the cost of a virtual function?

Answer

One hidden pointer per object (typically 8 bytes), one vtable per class, an indirect branch per call (harder for the CPU to predict), and usually no inlining through the virtual call. For most application code this is negligible; for tiny hot inner loops it can dominate — which is why math kernels and embedded code often use templates/CRTP instead.

Devirtualization lets the compiler convert provably-monomorphic virtual calls into direct calls, recovering the cost when the dynamic type is known.

Example

Example.cppcpp
// hot loop: template (devirtualized/inlined) beats virtual dispatch
template<class Op> void run(Op op, int* data, int n) {
    for (int i = 0; i < n; ++i) op(data[i]);
}

Why must a polymorphic base class have a virtual destructor?

Answer

Deleting a derived object through a base pointer with a non-virtual destructor runs only the base destructor — derived members leak and the behavior is undefined. A virtual destructor routes destruction through the vtable so the full derived-then-base chain runs.

Rule: any class with a virtual function should have a virtual (possibly defaulted) destructor; classes not meant as bases should not gain virtuals casually because of the vptr cost.

Example

Example.cppcpp
struct Base { virtual ~Base() = default; };
struct Derived : Base { std::string big; };
Base* p = new Derived();
delete p; // full Derived then Base destruction

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits