What the final keyword does
C++11 added the final keyword. You can put it in two places:
- After a class name, which stops other classes from inheriting from it.
- After a virtual function declaration, which stops derived classes from overriding that function.
Both checks happen at compile time. If code breaks the rule, the compiler reports an error before the program runs.
Final classes
To make a class final, add final after its name:
#include <iostream>
class Logger final {
public:
void log(const char* message) const {
std::cout << message << '\n';
}
};
// class FileLogger : public Logger { // Error: Logger is final
// };
int main() {
Logger logger;
logger.log("Hello from a final class");
}
You can still create and use a Logger object. What you can’t do is use it as a base class. The commented-out FileLogger line is the kind of code the compiler rejects.
A final class suits a class whose behavior should stay exactly as written. A small helper or a simple value type is a common example, since a subclass could change behavior that other code relies on.
Final virtual functions
If base and derived classes are new to you, brush up on inheritance in c first. Once that makes sense, final on a virtual function works the same way: mark the function final in the class where you want to stop overriding.
#include <iostream>
class Animal {
public:
virtual ~Animal() = default;
virtual void speak() const {
std::cout << "...\n";
}
};
class Dog : public Animal {
public:
void speak() const override final {
std::cout << "Woof\n";
}
};
class Puppy : public Dog {
public:
// void speak() const override { // Error: Dog::speak is final
// }
};
int main() {
Dog dog;
Animal& pet = dog;
pet.speak(); // Prints "Woof"
}
Dog overrides speak and marks it final. Puppy can still inherit from Dog and use speak as it is, but it can’t override it. The Animal& reference shows the normal virtual call: even through a base reference, the Dog version runs.
Using final with override
override asks the compiler to confirm that a function really overrides a virtual function in a base class. final stops any further overriding. You can use both on the same function:
void speak() const override final;
The two keywords can appear in either order, though override first is the common style.
Using override is a good habit even without final. If the signature doesn’t match the base function, for example because const is missing, the compiler reports an error instead of quietly creating a new function.
Common errors
- Using
finalon a function that isn’t virtual. The function must be virtual, either because it is declaredvirtualor because it overrides a virtual function in a base class. A plain member function can’t befinal. - Overriding a final function. A derived class that tries to override it gets a compile error.
- Deriving from a final class. The compiler rejects the derived class.
- Putting
finalon a variable. Unlike Java, C++ doesn’t allowfinal int limit = 10;. For values that shouldn’t change, useconstorconstexpr. For more on howconstworks, see the c const keyword guide. - Assuming
finalis reserved everywhere. It is a contextual keyword, so outside a class name or a virtual function declaration it is an ordinary name.int final = 5;compiles, though it is confusing and best avoided.
Final in other languages
If you’ve used Java, final on a class stops subclassing, and final on a method stops overriding. Java also uses final on variables, which C++ handles with const instead.
C# uses sealed for the same idea. A sealed class can’t be inherited, and a sealed override method can’t be overridden again in a further derived class.
When to use final
Consider final when:
- A class isn’t meant to be a base class, such as a small helper or a value type.
- A virtual function’s behavior must stay the same in every derived class, such as a fixed step in a process.
Some compilers may use this information to optimize virtual calls, but don’t add final just for speed. Measure first if performance is the goal.
Also decide early. Adding final to a class that already has derived classes breaks that code, so it is harder to add later.
Key takeaways
- The
finalkeyword, added in C++11, stops inheritance from a class or overriding of a virtual function. - Place it after the class name, or after a virtual function’s parameter list and any
const. - Pair it with
overridefor safer overrides. - Use
constfor values that shouldn’t change.finaldoesn’t apply to variables in C++.