Rule A10-3-1 (required, implementation, automated)
Virtual function declaration shall contain exactly one of the three specifiers: (1) virtual, (2) override, (3) final.
Rationale
Specifying more than one of these three specifiers along with virtual function declaration is redundant and a potential source of errors. It is recommended to use the virtual specifier only for new virtual function declaration, the override specifier for overrider declaration, and the final specifier for final overrider declaration. Note that this applies to virtual destructors and virtual operators, too.
Example
// $Id: A10-3-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
class A
{
public:
virtual
~A() {}
// Compliant
virtual
void F() noexcept = 0;
// Compliant
virtual void G() noexcept final = 0; // Non-compliant - virtual final pure
// function is redundant
virtual void
H() noexcept final // Non-compliant - function is virtual and final
{
}
virtual void K() noexcept // Compliant
{
}
virtual void J() noexcept {}
virtual void M() noexcept // Compliant
{
}
virtual void Z() noexcept // Compliant
{
}
virtual A& operator+=(A const& rhs) noexcept // Compliant
{
// ...
return *this;
}
};
class B : public A
{
public:
~B() override {}
// Compliant
virtual void F() noexcept override // Non-compliant - function is specified
// with virtual and override
{
}
void K() noexcept override
final // Non-compliant - function is specified with override and final
{
}
virtual void M() noexcept // Compliant - violates A10-3-2
{
}
void Z() noexcept override // Compliant
{
}
void J() noexcept // Non-compliant - virtual function but not marked as
// overrider
{
}
A& operator+=(A const& rhs) noexcept override // Compliant - to override
// the operator correctly,
// its
signature needs to be
// the same as in the base
// class
{
// ...
return *this;
}
};
See also
C++ Core Guidelines [11]: C.128: Virtual functions should specify exactly one of virtual, override, or final.