主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

MISRA C++:2008 Rule 11-0-1

Member data in non- POD class types shall be private

描述

规则定义

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

理由

如果类具有可公开访问的数据成员,则其他类和函数可能会直接与类数据成员交互。类中的任何更改都可能需要更新使用该类的客户端。如果一个类不是普通数据类型 (POD),限制对其数据成员的访问可以实现类的封装。在这种封装类中,类的实现细节对使用它的客户端是不可见的。该类保留对其实现的控制权,可以独立维护,不会影响使用该类的客户端。

Polyspace 实现

Polyspace® 在非 POD 类型的类中标记非私有数据成员。Polyspace 空格使用与标准相同的 POD 类定义。

故障排除

如果您预期会出现违规,而 Polyspace 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

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;
};

在此示例中,数据成员 yz 不是 private。Polyspace 会对它们进行标记。

检查信息

组:成员访问控制
类别:必需

版本历史记录

在 R2013b 中推出

全部展开


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.