AUTOSAR C++14 Rule A11-3-1
Friend declarations shall not be used
Description
Rule Definition
Friend declarations shall not be used.
Rationale
You declare a function as friend of a class to access private members of the class outside the class scope.
class A { int data; public: // operator+ can access private members of class A such as data friend A const operator+(A const& lhs, A const& rhs); };
Code with friend functions can be difficult to maintain. For instance, if class
myClass
has a friend class anotherClass
, when you
change a data member of myClass
, you have to find all instances of its
usage in member functions of anotherClass
.
Polyspace Implementation
The rule checker flags all uses of the friend
keyword.
The checker follows specifications of AUTOSAR C++ 14 release 18-03 (March 2018).
However, release 18-10 and later releases of AUTOSAR C++14 allows an exception for
comparison operators such as operator==
. If the rule checker flags the
use of comparison operators, add a comment justifying the result. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
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: Member Access Control |
Category: Required, Automated |
Version History
Introduced in R2019a