Details the use of the override and final specifiers for the C++ member function

  • 2020-05-07 20:13:42
  • OfStack

override specifier

The override keyword can be used to specify a member function that overrides a virtual function in a base class.
grammar


function-declaration override;

note
override is context-sensitive and has special meaning only when used after a member function has been declared. Otherwise, it is not the reserved keyword.
Using override helps prevent unexpected inheritance behavior in your code. The following example demonstrates that you may not intend to use the member function behavior of a derived class without override. The compiler does not emit any errors with this code.


class BaseClass
{
  virtual void funcA();
  virtual void funcB() const;
  virtual void funcC(int = 0);
  void funcD();
};

class DerivedClass: public BaseClass
{
  virtual void funcA(); // ok, works as intended

  virtual void funcB(); // DerivedClass::funcB() is non-const, so it does not
             // override BaseClass::funcB() const and it is a new member function

  virtual void funcC(double = 0.0); // DerivedClass::funcC(double) has a different
                   // parameter type than BaseClass::funcC(int), so
                   // DerivedClass::funcC(double) is a new member function

};

When override is used, the compiler generates errors instead of creating new member functions without prompting.


class BaseClass
{
  virtual void funcA();
  virtual void funcB() const;
  virtual void funcC(int = 0);
  void funcD();
};

class DerivedClass: public BaseClass
{
  virtual void funcA() override; // ok

  virtual void funcB() override; // compiler error: DerivedClass::funcB() does not 
                  // override BaseClass::funcB() const

  virtual void funcC( double = 0.0 ) override; // compiler error: 
                         // DerivedClass::funcC(double) does not 
                         // override BaseClass::funcC(int)

  void funcD() override; // compiler error: DerivedClass::funcD() does not 
              // override the non-virtual BaseClass::funcD()
};


To specify that functions cannot be overridden and classes cannot be inherited, use the final keyword.


final specifier
can specify virtual functions that cannot be overridden in derived classes using the final keyword. You can also use it to specify classes that cannot be inherited.
grammar


function-declaration final;

class class-name final base-classes

note
final is context-sensitive and has special meaning only when used after a function declaration or a class name; Otherwise, it is not the reserved keyword.
When final is used in a class declaration, base-classes is an optional part of the declaration.
The following example USES the final keyword to specify that virtual functions cannot be overridden.


class BaseClass
{
  virtual void func() final;
};

class DerivedClass: public BaseClass
{
  virtual void func(); // compiler error: attempting to 
             // override a final function
};

For information on how to specify that a member function can be overridden, see the override specifier.
The next example USES the final keyword to specify that the class cannot be inherited.


class BaseClass final 
{
};

class DerivedClass: public BaseClass // compiler error: BaseClass is 
                   // marked as non-inheritable
{
};


Related articles: