AUTOSAR C++14 Rule M9-3-3
If a member function can be made static then it shall be made static, otherwise if it can be made const then it shall be made const
Description
Rule Definition
If a member function can be made static then it shall be made static, otherwise if it can be made const then it shall be made const.
Rationale
const
member functions cannot modify the data members of the class.
static
member function cannot modify the nonstatic data members of the
class. If a member function does not need to modify the nonstatic data members of the class,
limit their access to data by declaring the member functions as const
or
static
. Such declaration clearly expresses and enforces the design
intent. That is, if you inadvertently attempt to modify a data member through a
const
member function, the compiler catches the error. Without the
const
declaration, this kind of inadvertent error might lead to bugs
that are difficult to find or debug.
Polyspace Implementation
The checker performs these checks in this order:
The checker first checks if a class member function accesses a data member of the class. Functions that do not access data members can be declared static.
The checker then checks functions that access data members to determine if the function modifies any of the data members. Functions that do not modify data members can be declared const.
A violation on a const
member function means that the function does
not access a data member of the class and can be declared static.
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: Classes |
Category: Required, Automated |