C++11 and override

Jun 26, 2014

The new override modifier can be applied to a virtual method in C++11, and
instructs the compiler that the method is intended to override a virtual
method defined in the parent class. The primary advantage is that typos and
mismatched method signatures that would have resulted in subtle bugs and
unintended runtime bheaviour before can now be detected at build time and
easily corrected.

Without override

Imagine we have the following code:

// Base class with virtual method
classBase0{public:virtualvoidfoo(intnum){std::cout<<"Base0::foo(int)"<<std::endl;}};// Derived class with mismatched type.
// This error will not be caught.
classDerived0:publicBase0{public:virtualvoidfoo(floatnum){std::cout<<"Derived0::foo(float)"<<std::endl;}};

The intent was for Derived0 to implement its own foo() method to
override the Base0::foo(int) method. However, the developer made a
mistake in the type signature and the parameter type does not match (float
here versus int above). The result is that Derived0::foo(float) is a
distinct method from Base0::foo(int) and will not necessarily be called on
all the occasions the developer intended. Typically this would be when a
polymorphic call on a Base0 pointer type to a Derived0 instance was
expected to call the derived implementation. In other words, given some
test code which exercises the foo method for each class, if we write:

The developer had intended that Derived0 be invoked in the second call,
but the Base0 version was called instead, since it matches the type
signature of the method in the base class and foo(float) is not considered
an overriding method.

With override

The fix is to add the override modifier to the method declaration in the
derived class to make our intentions explicit.

When to use override

The override modifier should be used in modern C++ code whenever a virtual
function is overridden in a derived class. The use of override can catch
some subtle bugs that would be otherwise be time-consuming and difficult to
detect and correct.

The override modifier only has a compile-time impact, so there is no
runtime cost whatsoever to using it.

There is one consideration - since override is a C++11 feature, care must
be taken to ensure that the target toolchain is compatible with modern
features. Older compilers would need a compatability mode to ignore this
modifier in order to build.

Sample Code

Final vs Override

In addition to the override modifier, there is support for expressing the
opposite sense; the final declaration instructs the compiler that the
method cannot be overridden by a dervied class. The final modifier is
explained in its own article, C++11 and final.