By using the context sensitive keyword override and final you can explicit manage the overriding of virtual functions. In particular, the keyword override solves a lot of issues with difficult to finding bugs in object hierarchies: Methods that should override methods of base classes. The result is a syntactically but not semantically correct program. The program performs the wrong stuff in the right way.
To override a method, the signature of the overriding method has to match exactly. What sounds easy is often not so easy in practice: If the signature of the method fits not exactly, you will get a correct program with the wrong behaviour. That's simple, a different method will be invoked.
The name override in the method declaration expresses that the method should override a virtual method of a base class. The compiler checks the assertion. It checks the parameter of the method, the return type of the method, and qualifiers like const and volatile. Of course, the compiler notices if the overridden method is not virtual.
If you compile the program, the compiler will complain a lot. The error message is very specific.
The compiler complains in line 16 that the method func1 is not overriding a method.The same hold true for the func2. It has the wrong parameter type. It goes on with the method func3. func3 has no const qualifier. func4 has the wrong return type. Only the method f in line 24 did it right and overrides the method f of their base class.
It's a job for final If a virtual method should not be overridden.
final supports two use cases. First, you can declare a method that can not be overridden; second, you can define a class from which you can not derive. The compiler uses the same rules as in the case of override in order to determine if a method overrides a method of a base class. Of course, the strategy goes the other way around because final should suppress the overriding of a method. Therefore, the compiler checks the parameter of the method, their return type, and the const/volatile qualifiers.
What's happening at compile time?
The compiler does its job very neat. It complains that the method h in the class Base (line 4) is overridden by the method in class Derived (line 9). Of course it's okay that the method h (line 10) in class Derived overloads f for the parameter type double. Quite interesting is the method g (line 11) in the class Derived. The method overrides the method g (line 5) of the class Base and declares the method final. Therefore, g can not be overridden in DerivedDerived (line 15).
To the class FinalClass (line 18). DerivedClass can not be derived from FinalClass, because the BaseClass is final.
I ignored intentionally one fact. The keywords override and final are context sensitive keywords. What does that mean?
Context sensitive keywords
override and final are only keywords in specific contexts. These contexts are the declaration of a method or of a class. If you use them in other contexts, they will be identifiers. What was the reason for introduce context sensitive keywords into the C++ standard? On one hand, the C++ standardization committee don't like it to introduce new keywords; on the other hand, the classical programs keeps valid if they use the context sensitive keywords override and final. With classical programs I mean a program written with C++98/C++03 syntax in mind.
Context sensitive keywords following a key principle of C++: Don't break existing code.
My small program is valid C++ although I named the function override (line 5) and gave the variable the name final (line 13).
Only for the sake of completeness. C++11 has with default and delete additional context sensitive keywords.
The new keyword nullptr defines a null pointer constant in C++11. nullptr clears the ambiguity of the number 0 in C++ and the macro NULL in C. How? You have to wait for the next post.
Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library". Get your e-book. Support my blog.