Skip to content
Tutorials Center
My learning

Topics

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.

C++
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

C++ override and final in action

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.

C++ The mistake override catches (fails on purpose)

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

C++
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 override on every overriding function. It documents intent and turns the classic mistakes into errors: a missing const, a different parameter type (int versus long), 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/volatile qualifiers, ref-qualifiers and, for return types, be identical or covariant (a pointer or reference to a derived class). The noexcept rule is looser: an overrider may not be less strict than the base declaration.
  • An overriding function is automatically virtual even if you leave virtual off. The usual style is virtual on the first declaration in the hierarchy and override (without virtual) afterwards.
  • override and final are not part of the function's type and do not change the generated code by themselves. final can, however, help the compiler devirtualize calls: a call through a Cube& can skip the vtable, because nothing further can override.
  • final on 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 override too (~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 with override as well, and you can combine override with = default for a destructor.