Virtual functions
A virtual function is dispatched on the object's dynamic type, not on the type of the pointer or reference you call through. base->draw() runs Circle::draw if the object is a Circle and draw was declared virtual in Shape.
Detailed Explanation
Without virtual, a member call is resolved at compile time from the static type. Shape* p = &circle; p->draw(); calls Shape::draw even if Circle wrote its own draw. That is the right default: it is a normal function call, inlinable, with no extra word in the object.
virtual on the base declaration switches the call to dynamic dispatch. The object stores a pointer to a vtable (see that page). The call loads the function pointer for draw from the table that belongs to the real type and calls it. Circle "overrides" Shape::draw by declaring a function with the same name, parameters, and (compatible) return type.
Write virtual once, on the first declaration in the hierarchy. In the derived class write override (C++11). override is a compile-time check: if the signature does not match a virtual in a base, the program does not compile. Forgetting override and mismatching a const or a parameter silently declares a new function and the base call still hits the base version.
final on a virtual function forbids further overrides. final on a class forbids further inheritance. Use them when a type is not designed as a base, so nobody adds a vtable requirement later.
A virtual destructor is a virtual function. If you delete through a base pointer, the destructor must be virtual or the derived destructor will not run. If you do not delete through the base, you can keep the destructor non-virtual and avoid a vtable if the class has no other virtuals.
Default arguments are bound statically. virtual void log(int level = 1); overridden as void log(int level = 2) still uses 1 when called through the base. Put default arguments only on the base declaration, or do not use them on virtuals.
Virtual calls from a constructor or destructor do not reach derived overrides. While Base's constructor runs, the object is a Base; its vtable points at Base's functions. Calling virtual setup() from Base() will not run Derived::setup. Pass construction data down, or run a two-phase init after the derived constructor finishes.
Cost: one extra pointer per object (the vptr), one extra load per call, and a barrier to inlining across the virtual. On a hot numeric loop that is measurable. On a draw() called a thousand times per frame it is usually noise compared to the work inside draw. Do not make every function virtual "for future flexibility"; make the ones that are the extension points.
Code Examples
#include <iostream>
#include <memory>
#include <vector>
struct Shape {
virtual ~Shape() = default;
virtual void draw() const { std::cout << "shape\n"; }
};
struct Circle : Shape {
void draw() const override { std::cout << "circle\n"; }
};
int main() {
std::vector<std::unique_ptr<Shape>> scene;
scene.push_back(std::make_unique<Shape>());
scene.push_back(std::make_unique<Circle>());
for (const auto& s : scene) s->draw();
// prints:
// shape
// circle
}struct Base {
virtual void f(int n) const;
};
struct Derived : Base {
// void f(int n); // different: missing const, does not override
void f(int n) const override;
};Common Gotchas
Missing `override` hides a signature mismatch
an extra `const`, a `std::string` versus `std::string_view`, or a different reference qualifier creates a new function. The virtual call still hits the base. Always write `override`.
Virtual calls in constructors see the base
the derived object is not formed yet. Do not call virtual `init` from `Base()`.
Default arguments come from the static type
callers using a `Base*` get `Base`'s defaults even if `Derived` wrote different ones.
A non-virtual function in the derived hides the base name
`using Base::f;` if you need overloads from both.