AUTOSAR C++14 Rule A10-3-1
Virtual function declaration shall contain exactly one of the three specifiers: (1) virtual, (2) override, (3) final
Since R2020a
Description
Rule Definition
Virtual function declaration shall contain exactly one of the three specifiers: (1) virtual, (2) override, (3) final.
Rationale
Virtual functions implement polymorphic behavior in a class hierarchy. Once you declare
a function as virtual
in a base class, all instances of the function with
an identical parameter list in the derived classes override the base function implicitly. If
you rely on this implicit action by the compiler for implementing polymorphic functions, it
can lead to errors. For instance:
A function can become inadvertently
virtual
because its signature matches a virtual function in the base class.A function can become inadvertently non-virtual because there are differences in the parameter list.
Implicitly declaring virtual functions can also make the code hard to read.
To avoid inadvertent errors and to enhance readability, use the specifiers
virtual
, override
, or final
to
explicitly define virtual or overriding functions. Because using more than one of these
specifiers in a declaration is either redundant or a source of error, use exactly one of
these specifiers:
Only
virtual
to declare a new virtual function.Only
override
to declare a non-final overriding function of a virtual function.Only
final
to declare a final overriding function of a virtual function.
Polyspace Implementation
Polyspace® flags declaration of virtual functions if:
The declaration uses none of the specifiers.
The declaration uses more than one of the specifiers.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Derived classes |
Category: Required, Automated |
Version History
Introduced in R2020a