V2023. Using 'override' ensures that a function signature is correct when overriding a virtual function.
This diagnostic rule was added at the users' request.
The rule is designed to detect all cases of overriding virtual functions of a base class that do not use the override specifier.
Note 1. To search only for incorrect virtual function overrides, use the diagnostic rule V762.
Note 2. This diagnostic rule is relevant only for C++.
Although the absence of the override specifier is not an error, it can reduce code readability and lead to potential errors in the future. By explicitly specifying override, the developer clearly indicates that the function overrides a base class virtual function. Without this specifier, there might be a misunderstanding and the function might be perceived as belonging to a child class.
Using override also provides additional safety by ensuring that the override is performed correctly. If the function name or signature does not match any virtual function in the base class, the compiler throws an error. Without override, the code is compiled, but a new function is created, which can lead to unexpected program behavior.
The example of non-compliant code:
class A
{
public:
virtual void one() = 0;
virtual void two();
};
class B : public A
{
void one(); // <=
virtual void two(); // <=
};
The example of the fixed code that uses the override specifier:
class A
{
public:
virtual void one() = 0;
virtual void two();
};
class B : public A
{
void one() override; // ok
void two() override; // ok
};