AUTOSAR C++14 Rule A10-3-2
Each overriding virtual function shall be declared with the override or final specifier
Since R2020a
Description
Rule Definition
Each overriding virtual function shall be declared with the override or final specifier.
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 overriding virtual functions can also make the code hard to read.
To avoid inadvertent errors and to enhance readability, use the specifiers
override
and final
explicitly in every declaration
of overriding functions.
Polyspace Implementation
Polyspace® flags the declarations of virtual
functions if all of these
statements are true:
The function is in a derived class.
The signature of the function matches the signature of a virtual function in the base class.
The declaration of the function lacks the specifier
override
orfinal
.
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