AutosarC++19_03-A10.3.1¶
Virtual function declaration shall contain exactly one of the three specifiers: (1) virtual, (2) override, (3) final
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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.
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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
detailed_message¶
detailed_message : bool = False
disable_for_destructors¶
disable_for_destructors : bool = False