Quiz Entry - updated: 2026.07.06
What does virtual mean in C++?
virtual enables runtime (dynamic) dispatch — the override matching the object's actual type is called, not the one for the pointer's declared type.
* Dynamic dispatch at work: the object carries a vptr to its type's vtable, so v->getName() resolves to Truck's override at runtime. *
class Vehicle {
virtual string getName() { return "Vehicle"; }
};
class Truck : public Vehicle {
virtual string getName() { return "Truck"; } // override
};
Vehicle *v = new Truck();
v->getName(); // returns "Truck", not "Vehicle"!
Without virtual:
- The call is resolved at compile time (static binding) based on the pointer's declared type —
Vehicle's version would run.
With virtual:
- The call is resolved at runtime (dynamic binding) based on the real object —
Truck's version runs.
Tip: Under the hood the compiler builds a vtable (a table of function pointers per type) and the object carries a hidden pointer to it — a detail that matters a lot in reverse engineering C++ binaries.
Go deeper:
virtual function specifier — cppreference — the exact override and dispatch rules.
Virtual method table — Wikipedia — how the vtable is laid out in memory.