Site icon Learn C++

Learn How To Use The Final Specifier In Modern C++

Final Specifier In Modern C++

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:

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:

[crayon-663c697a67c7f891271980/]

or with the body,

[crayon-663c697a67c85662791982/]

or with virtual function declaration,

[crayon-663c697a67c87275462943/]

and, here is an example for the Clang-enhanced C++ compilers

[crayon-663c697a67c88770106371/]

in previous-generation compilers, it was used with the [[final]] keyword like this:

[crayon-663c697a67c8a591083673/]

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:

[crayon-663c697a67c94393289157/]

or with other base classes as below,

[crayon-663c697a67c95425357217/]

this is how we can use it:

[crayon-663c697a67c99324728339/]

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.

Exit mobile version