Main Content

MISRA C++:2023 Rule 14.1.1

Non-static data members should be either all private or all public

Since R2024b

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 and public 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

expand all

In this example, the class Base defines protectedData as a protected data member. Polyspace reports a violation of this rule.

// Example of a class with protected data members in C++

class Base { //Noncompliant
protected:
	int protectedData;

public:
	// Constructor to initialize protected data
	Base(int value) : protectedData(value) {}

	// Getter for protected data
	int getProtectedData() const {
		return protectedData;
	}
};

class Derived : public Base {
public:
	// Constructor to initialize base class data
	Derived(int value) : Base(value) {}

	// Function to manipulate protected data
	void manipulateData(int value) {
		// Accessing protected member from derived class
		protectedData = value;
	}
};

Correction — Use protected Functions

To fix this violation, make the data member private. Use protected functions in the class Base to allow the class Derived privileged access the data member.

// Example of a class with private data members in C++

class Base {
private:
    int protectedData;

protected:
    // Protected setter for derived class to access private data
    void setProtectedData(int value) {
        protectedData = value;
    }

    // Protected getter for derived class to access private data
    int getProtectedData() const {
        return protectedData;
    }

public:
    // Constructor to initialize private data
    Base(int value) : protectedData(value) {}
};

class Derived : public Base {
public:
    // Constructor to initialize base class data
    Derived(int value) : Base(value) {}

    // Function to manipulate private data through protected functions
    void manipulateData(int value) {
        // Accessing private member via protected function from derived class
        setProtectedData(value);
    }

    // Function to get the value of private data through protected functions
    int retrieveData() const {
        // Accessing private member via protected function from derived class
        return getProtectedData();
    }
};

In this example, the Example class uses both private and public data members. Polyspace reports a violation of this rule.

// Example of a class with both public and private data members in C++

class Example {  //Noncompliant
private:
	int privateData;  // Private data member

public:
	int publicData;   // Public data member

	// Constructor to initialize data members
	Example(int privateValue, int publicValue)
		: privateData(privateValue), publicData(publicValue) {}

	// Getter for private data
	int getPrivateData() const {
		return privateData;
	}

	// Setter for private data
	void setPrivateData(int value) {
		privateData = value;
	}
};

Check Information

Group: Member access control
Category: Advisory

Version History

Introduced in R2024b