C++11 gives us a way to make explicit that a derived class function is supposed to override a base class version: override
.
There are several requirements for overriding to occur:
- The base class function must be virtual
- The base and derived funciton names must be identical (except in the case of destructors)
- The parameter types of the base and derived functions must be identical
- The
const
ness of the base and derived functions must be identical - The return types and exception specifications of the base and derived functions must be compatible
- The functions’ reference qualifiers mmust be identical (new from C++11)
All these requirements for overriding mean that small mistakes can make a big difference. Code containing unintended overriding errors is typically still valid, so compilers may fail to notify us the errors. For example, following code is completely legal:
|
|
However, we almost certainly intend to override base class functions with derived class ones with the same names, yet none of the derived class functions are tied to the base class ones:
mf1
is declaredconst
inBase
, but not inDerived
mf2
takes anint
inBase
, but anunsigned int
inDerived
mf3
is lvalue-qualified inBase
, butrvalue-qualified
inDerived
mf4
isn’t declaredvirtual
inBase
If we explicitly declare with the contextual keyword override
:
|
|
Then the code won’t compile, because compilers will complain the overriding-related problems above, which is exactly what we want.
Moreover, taking use of compilers’ ability to diagnostic overriding problems, we can easily use override
keyword to gauge the ramifications if we’re contemplating changing the signature of a virtual funciton in a base class: if derived classes use override
everywhere, we can just change the signature, recompile the system, see how much damage we’ve caused, and then decide whether the signature change is worth the trouble.