Main Content

AUTOSAR C++14 Rule M11-0-1

Member data in non-POD class types shall be private

Description

Rule Definition

Member data in non-POD class types shall be private.

Rationale

If classes have data members that are publicly accessible, other classes and functions might interact with the class data members directly. Any change in the class might require updating the clients that use the class. If a class is not a plain-old-data (POD) type, restricting access to its data members enables encapsulation of the class. In such an encapsulated class, the implementation details of the class are opaque to the clients that use it. The class retains control over its implementation and can be maintained independently without impacting the clients that use the class.

Polyspace Implementation

Polyspace® flags nonprivate data members in classes that are not POD types. Polyspace space uses the same definition of POD classes as the standard.

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

expand all

class nonPOD{
	nonPOD(){
		//...
	}
	~nonPOD(){
		//...
	}
	public:
	int getX();
	int setX(int&);
	int getY();
	int setY(int&);
	int getZ();
	int setZ(int&);
	int x; //Noncompliant
	protected:
	int y; //Noncompliant
	private:
	int z;
};

In this example, the data members y and z are not private. Polyspace flags them.

Check Information

Group: Member Access Control
Category: Required, Automated

Version History

Introduced in R2019a

expand all