C++ Reference ยท Keywords and Statements
The override and final specifiers
Reference for C++ override and final: catching signature mistakes at compile time, sealing virtual functions and classes, and how they interact with virtual.
override and final are contextual specifiers: they only have special meaning in the places described here, so they remain valid identifiers elsewhere. Both were added in C++11 to make virtual functions harder to get wrong.
Put override after a member function declaration to say "this must replace a virtual function from a base class". If nothing in any base has a matching signature, the program does not compile. Put final on a virtual function to forbid further overriding, or after a class name to forbid inheriting from that class at all.
struct Base { virtual void draw() const; }; struct Circle final : Base { // nobody may derive from Circle void draw() const override; // must match Base::draw exactly };
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
return-type name(params) cv ref noexcept override; return-type name(params) cv ref noexcept final; return-type name(params) cv ref noexcept override final; // either order class Name final : public Base { /* ... */ }; // 'final' after the class name
| Specifier | Where | Meaning |
|---|---|---|
override |
After the declarator of a member function | The function must override a virtual function of a base class; otherwise ill-formed |
final (function) |
After the declarator of a virtual function | No derived class may override it |
final (class) |
After the class name in the class head | No class may derive from it |
Errors. override on a function that does not override anything is a compile-time error (in the second example, GCC reports that the function "marked 'override', but does not override"). Overriding a final function, or deriving from a final class, is also a compile-time error.
Usage notes
- Use
overrideon every overriding function. It documents intent and turns the classic mistakes into errors: a missingconst, a different parameter type (intversuslong), a typo in the name, or a base function that was later renamed or made non-virtual. - The overrider must match the base function in parameter types,
const/volatilequalifiers, ref-qualifiers and, for return types, be identical or covariant (a pointer or reference to a derived class). Thenoexceptrule is looser: an overrider may not be less strict than the base declaration. - An overriding function is automatically virtual even if you leave
virtualoff. The usual style isvirtualon the first declaration in the hierarchy andoverride(withoutvirtual) afterwards. overrideandfinalare not part of the function's type and do not change the generated code by themselves.finalcan, however, help the compiler devirtualize calls: a call through aCube&can skip the vtable, because nothing further can override.finalon a class is a design statement. Use it for types not designed for inheritance (many standard-library-like value types). It also stops accidental slicing patterns through derivation.- A destructor can be
overridetoo (~Derived() override;), which verifies that the base destructor is virtual. - Because they are contextual,
int override = 1;is legal, though you should never write it. - Pure virtual functions (
= 0) can be overridden withoverrideas well, and you can combineoverridewith= defaultfor a destructor.