C++ Reference ยท Keywords and Statements
The virtual keyword
Reference for C++ virtual: virtual member functions, dynamic dispatch, pure virtual functions, virtual destructors, virtual inheritance and calls in constructors.
virtual enables runtime polymorphism. A call to a virtual member function through a pointer or reference is resolved using the dynamic type of the object, not the declared type of the pointer. A Shape* that actually points to a Circle calls Circle::area().
Typical implementations store a hidden pointer in each polymorphic object that leads to a table of function addresses (the vtable). A virtual call costs an indirect jump and usually prevents inlining. virtual also appears in base-class lists (virtual inheritance) to share one copy of a base in diamond hierarchies.
struct Shape { virtual double area() const = 0; // pure virtual: no body needed virtual ~Shape() = default; // virtual destructor };
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 return-type f(params) [const] [noexcept]; // virtual member function virtual return-type f(params) = 0; // pure virtual virtual ~Class(); // virtual destructor class D : virtual public B { }; // virtual base class
| Form | Description |
|---|---|
| Virtual function | Overridable. A derived function with the same name, parameter types, cv- and ref-qualifiers overrides it, and is virtual automatically (write override to have the compiler check). |
Pure virtual (= 0) |
Makes the class abstract: it cannot be instantiated. May still have a body, defined outside the class, that derived classes call explicitly. |
| Virtual destructor | Required if you delete a derived object through a base pointer. |
| Virtual base | Shared single subobject across multiple inheritance paths; the most derived class constructs it. |
Usage notes
- Dispatch happens only through pointers and references. Passing a
Circleby value to aShapeparameter slices it, and no virtual behavior survives. - A class with a virtual function should have a virtual destructor (or a protected non-virtual one). Deleting a derived object through a base pointer without it is undefined behavior.
- Calls to virtual functions inside constructors and destructors do not reach derived overrides: while
Baseis being built, the object's dynamic type isBase. - Overriding functions may return a more derived pointer or reference (covariant return types), but not different value types.
- Default arguments are chosen by the static type, not the dynamic one. Do not give an override different defaults.
- Use
overridealways andfinalto stop further overriding; see override and final. - Templates cannot be virtual: a member function template cannot be
virtual. - A virtual call cannot be inlined unless the compiler can prove the dynamic type (devirtualization). A
finalclass helps. - Polymorphic objects gain a hidden pointer (typically 8 bytes). Virtual inheritance adds another per virtual base.
- Starting with C++20,
virtualfunctions can beconstexpr.