C++11/C++14 override and final identifiers- 2020
"If you're an experienced C++ programmer and are anything like me, you initially
approached C++11 thinking, "Yes, yes, I get it. It's C++, only more so." But as you
learned more, you were surprised by the scope of the changes. auto declarations,
range-based for loops, lambda expressions, and rvalue references change the face of
C++, to say nothing of the new concurrency features. And then there are the
idiomatic changes. 0 and typedefs are out, nullptr and alias declarations are in.
Enums should now be scoped. Smart pointers are now preferable to built-in ones.
Moving objects is normally better than copying them.
- Effective Modern C++ by Scott Meyers
In C++03, it is possible to accidentally create a new virtual function, when one intended to override a base class function. For example:
// C++03 class Base { virtual void f(int); }; class Derived : public Base { virtual void f(float); };
The intention was to override Base::f() and to use Derived::f(). But instead, because it has a different signature, it creates a second virtual function.
// C++11 class Base { virtual void f(int); }; class Derived : public Base { virtual void f(float) override; // Error };
The override special identifier means that the compiler will check the base class(es) to see if there is a virtual function with this exact signature. And if there is not, the compiler will indicate an error.
C++11 also adds the ability to prevent inheriting from classes or simply preventing overriding methods in derived classes. This is done with the special identifier final. For example:
// no class can be derived from class A class A final { virtual void f(int); }; class B { // no class can override f() virtual void f() final; };
So, if we do the following, the compiler gives us errors:
class C : public A {}; // Error class D : public B // Error { virtual void f(); };
Note that override and final are not language keywords. They are technically identifiers for declarator attributes.
C++11/C++14 New Features
initializer_list
Uniform initialization
Type Inference (auto) and Range-based for loop
The nullptr and strongly typed enumerations
Static assertions and Constructor delegation
override and final
default and delete specifier
constexpr and string literals
Lambda functions and expressions
std::array container
Rvalue and Lvalue (from C++11 Thread tutorial)
Move semantics and Rvalue Reference (from C++11 Thread tutorial)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization