C++ Reference ยท Types and Language Concepts
Polymorphism and virtual dispatch
Reference for C++ runtime polymorphism: virtual functions, override and final, virtual destructors, dynamic dispatch through references, vtables and static alternatives.
Polymorphism means one interface, many behaviors. In C++ runtime polymorphism works through virtual functions: when you call a virtual member through a base-class pointer or reference, the program runs the version belonging to the object's actual (dynamic) type, not the declared type of the pointer.
To use it you need a base class with virtual functions, derived classes that override them, and access through a pointer or reference. Compile-time polymorphism (templates, overloading, concepts) achieves a similar goal without runtime cost, and is the right choice when the types are known at build time.
struct Shape { virtual double area() const = 0; virtual ~Shape() = default; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
virtual ReturnType name(params) const; // in the base class ReturnType name(params) const override; // in the derived class ReturnType name(params) const final; // no further overrides virtual ReturnType name(params) = 0; // pure virtual virtual ~Base() = default; // virtual destructor
| Part | Description |
|---|---|
virtual |
Enables dynamic dispatch for this function and every override of it in derived classes. |
override |
Asks the compiler to check that the function really overrides a base virtual. Always use it. |
final |
Forbids further overriding (or, on a class, deriving). |
= 0 |
Pure virtual: no required body; the class becomes abstract. |
An override must match the base signature exactly (parameter types, const, ref-qualifiers); the return type may be covariant (a pointer or reference to a more derived class).
Returns: as declared. Dispatch picks the final overrider for the object's dynamic type.
Errors / undefined behavior: deleting through a base pointer without a virtual destructor, calling a pure virtual function from a constructor or destructor, and using static_cast to a wrong derived type.
Usage notes
- Dispatch needs a pointer or reference. Passing by value slices the object, and the copy has the base's type. Use
const Base&or a smart pointer. - Always write
override. A typo in the signature (a missingconst,intversuslong) silently creates a new virtual function instead of overriding;overridemakes it a compile error. - Virtual destructor whenever a class is deleted through a base pointer. Giving the base
virtual ~Base() = default;costs one vtable pointer and is the safe default for polymorphic bases. - Constructors and destructors run with the dynamic type set to the class being constructed or destroyed, so a virtual call there reaches the current class's version.
- How it works. Most implementations store a hidden pointer to a per-class table of function addresses (the vtable) in each object, so a virtual call is one extra indirection and usually blocks inlining. Treat this as an implementation detail, not a guarantee.
- Default arguments are bound statically from the declared type of the call, not dynamically. Avoid defaults on virtual functions.
- Non-virtual hiding. A function with the same name but not virtual in the base does not override; the call resolves from the static type.
- Runtime type information:
dynamic_castandtypeidwork on polymorphic types (classes with at least one virtual function). - Alternatives: templates and concepts for compile-time polymorphism;
std::variantwithstd::visitfor a closed set of types;std::functionfor single-function interfaces.