AutosarC++17_03-A10.3.1

Virtual function declaration shall contain exactly one of the three specifiers: (1) virtual, (2) override, (3) final

Required inputs: IR

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 271687 2017-03-23 08:57:35Z piotr.tanski $
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 [10]: C.128: Virtual functions should specify exactly one of virtual, override, or final.

Possible Messages

Key

Text

Severity

Disabled

invalid_combination

Use only one of (1) virtual, (2) override, (3) final.

None

False

invalid_combination_nonoverriding

Use virtual when not overriding, but neither override nor final.

None

False

invalid_combination_overriding

Use either override or final when overriding, but not virtual.

None

False

Options

detailed_message

detailed_message : bool = False

If set, message description depends on whether function is overriding or not.
 

disable_for_destructors

disable_for_destructors : bool = False

If set, destructors are not checked.