Added support for 'final' and 'override' modifiers for classes, structures and functions.
'final' modifier for classes and structures
The presence of the 'final' modifier when declaring a structure or
a class prohibits the further inheritance from it. If there is no need
to make any further changes in the class (structure) or such changes are
unacceptable for security reasons, declare that class (structure) with
the 'final' modifier. In this case, all class methods are also
implicitly considered 'final'.
class CFoo final
{
};
class CBar : public CFoo
{
};
When attempting to inherit from a class with the 'final' modifier as shown above, the compiler displays an error:
cannot inherit from 'CFoo' as it has been declared as 'final'
see declaration of 'CFoo'
'override' modifier for functions
The 'override' modifier means that a declared function should
always override the parent class method. Using the modifiers allows you
to avoid errors when overriding, such as accidental change of a method
signature. For example, the 'func' method accepting the 'int' type
variable is defined in the base class:
class CFoo
{
void virtual func(int x) const { }
};
The method is overridden in the inherited class:
class CBar : public CFoo
{
void func(short x) { }
};
But the argument type is mistakenly changed from 'int' to 'short'.
In fact, the method overload instead of overriding is performed in that
case. While acting according to the overloaded function definition algorithm, the compiler may in some cases select a method defined in the base class instead of an overridden one.
In order to avoid such errors, the 'override' modifier should be explicitly added to the overridden method.
class CBar : public CFoo
{
void func(short x) override { }
};
If the method signature is changed during the overriding process,
the compiler cannot find the method with the same signature in the
parent class issuing the compilation error:
'CBar::func' method is declared with 'override' specifier but does not override any base class method
'final' modifier for functions
The 'final' modifier acts in the opposite way — it disables method
overriding in derived classes. If the method implementation is
self-sufficient and fully completed, declare it with the 'final'
modifier to ensure it is not changed later.
class CFoo
{
void virtual func(int x) final { }
};
class CBar : public CFoo
{
void func(int) { }
};
When attempting to override a method with the 'final' modifier as shown above, the compiler displays an error:
'CFoo::func' method declared as 'final' cannot be overridden by 'CBar::func'
see declaration of 'CFoo::func'