liveBook · Manning

Final

As explained in Learn C++ by Example

The final specifier is a C++ specifier that prevents a virtual function from being overridden or a class from being used as a base class.

The final specifier prevents a virtual function from being overridden or prevents a class from being used as a base class. section 6.1.2 section 6.2.4

Use final in two related ways:

  • Place it after a virtual member-function declaration to reject further overrides. section 6.1.2
  • Place it after a class name to make the class non-derivable; attempting to inherit from it fails to compile. section 6.2.4

A final class in the race example

Declaring `class StepperBlob final : public Blob` prevents a `DerivedStepper` class from using `StepperBlob` as its base. The compiler rejects that derived class. section 6.2.4

class StepperBlob final : public Blob

Inspect the class declaration that makes StepperBlob impossible to derive from. section 6.2.4

How Learn C++ by Example covers it

How deep it goes
Covers function and class restrictions with compile-time consequences, but does not develop implementation internals.
Examples
Uses StepperBlob, DerivedStepper, and a race example to demonstrate class-level inheritance prevention.
What it assumes
Assumes virtual functions, derived classes, and overriding; distinguishes correct overriding from hiding.
What you can do after
Apply it via C++ declarations to stop selected overrides or prevent a class from becoming a base class.
How it connects
Connects final with virtual functions, overriding, inheritance, polymorphism, and the override specifier.

Also explained in

Compare how 3 books explain final →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.