MISRA C++:2023 Rule 14.1.1
Description
Rule Definition
Non-static data members should be either all private
or all
public
.
Rationale
Implementing the interface of a class using member functions and then making the
non-static data members private
can help control the state of the class.
For example, if the data members of a class are private
, they cannot be
accessed by other objects through pointers or references. This can help prevent dangling
addresses and make detection of dangling addresses easier.
If a class does not need to enforce an invariant and only aggregates non-static data
members in a structure, making the data members public
simplifies the
code. In such a case, you can directly access the data members without requiring getter and
setter functions.
To prevent needlessly exposing the data members to derived classes, avoid using
protected
data members. If derived classes require privileged access to
data members, make the data members private
and create
protected
member functions to provide controlled access to derived
classes.
Polyspace Implementation
Polyspace® reports a violation if either of these conditions are true:
A class uses the access specifier
protected
for its non-static data members.A class uses a combination of
private
andpublic
access specifiers for its non-static data members.
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: Advisory |
Version History
Introduced in R2024b