MISRA C++:2023 Rule 14.1.1
Description
Rule Definition
Non-static data members should be either all private or all
public.
1
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
protectedfor its non-static data members.A class uses a combination of
privateandpublicaccess 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
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.