C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

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:

  • 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:

or with the body,

or with virtual function declaration,

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

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

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:

or with other base classes as below,

this is how we can use it:

For more information on this feature, see Explicit virtual overrides Proposal document.

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?