Modern C++ has many additions compared to the original C++ standard. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise. To achieve this goal C++11 introduces two new contextual keywords, the final and the override. The final specifier (keyword) is used for a function or for a class that cannot be derived and overridden by derived classes. This final specifier is used with the C++ compiler that has C++11 and the other higher C++ standards. In this post, we explain how to use the final specifier in modern C++.
What Is the final specifier in C++?
The final specifier (keyword) is used for a function or for a class that cannot be overridden by derived classes. Regarding virtual overrides, C++11 tends to tighten the rules, to detect some problems that often arise.
To achieve this goal C++11 introduces two new contextual keywords:
- final specifies that a method cannot be overridden, or a class cannot be derived.
- override specifies that a method overrides a virtual method declared in one of its parent classes.
In this post, we explain how to use the final specifier in C++.
The final specifier is and Explicit Virtual Override that prevents a class from being further inherited or prevents a function from being overridden. We can add the final specifier to a class definition or to a virtual member function declaration inside a class definition.
A class with the final specifier is not allowed to be a base class for another class. A virtual function with the final specifier cannot be overridden in a derived class. If a virtual member function f in some class B is marked final and in a class D derived from B, a function D::f
overrides B::f
, the program is ill-formed (the compiler does not issue a message).
How to use the final specifier in C++ functions?
The final specifier is used to designate virtual functions that cannot be overridden in a derived class. Here are the syntaxes of how to use it:
1 2 3 |
function_declaration final; |
or with the body,
1 2 3 4 5 |
function_declaration final { } |
or with virtual function declaration,
1 2 3 |
virtual function_declaration final; |
and, here is an example for the Clang-enhanced C++ compilers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> class Tx { virtual void f() final; }; class Ty : public Tx { virtual void f(); // ERROR: declaration of 'f' overrides a 'final' function }; int main() { Ty o1; return 0; } |
in previous-generation compilers, it was used with the [[final]]
keyword like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> class Tx { virtual void f() [[final]]; }; class Ty : public Tx { virtual void f(); // ERROR: declaration of 'f' overrides a 'final' function }; int main() { Ty o1; return 0; } |
If you need an official docwiki, please check this: Workaround for C++11 attributes
How to use the final specifier in modern C++ classes?
The final specifier is used to designate classes that cannot be inherited. Here are the syntaxes about how to use it:
1 2 3 |
class class_name final |
or with other base classes as below,
1 2 3 |
class class_name final base_classes |
this is how we can use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> class Ta final { }; class Tb : Ta // ERROR: base 'ta' is marked 'final' { }; int main() { Tb o1; return 0; } |
For more information on this feature, see Explicit virtual overrides Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition