override and final are two new qualifiers introduced in C++11. The use of these qualifiers is optional. They are meant to be used with virtual methods to show the intention of the method. The compiler uses these qualifiers to check if your intention matches the actual ground truth in your code and throws a compile error if it does not. Thus, it helps to catch bugs earlier at compile time.
- When you specify override for a method, you are indicating to the compiler that this is a virtual method and it is overriding a virtual method with the same signature in one of the base classes that the current class inherits from. If the method is not inheriting from any virtual method with the same signature, the compiler throws an error. Thus if you made a mistake in the function signature while defining this method, you would not have caught it unless you used this qualifier.
When you specify final for a method, you are indicating that this is a virtual method and that no class that inherits from the current class can override this method. If any method tries to override this method in an inherited class, the compiler throws an error.
If override or final are used with non-virtual methods, the compiler throws an error.
These qualifiers are specified after the function input arguments and should be specified after
constif the virtual method is a const method. If you put these qualifiers before a
const, you will get a weird error with GCC that gives no hint that this is because of the order of qualifiers is wrong!
These qualifiers are to be specified only with the method declaration. If you try to use them with the method definition, the compiler will throw an error.
You can specify
override finalfor a method, but it is the same as using
overrideis not allowed to be used with the base virtual method. This is for the obvious reason that the base virtual method is the first virtual method and it is not overriding any other method.
finalcan be used with the base virtual method. This can be used to specify that the first base virtual method cannot be overridden in any inherited class.
This code example shows how to use these qualifiers: