<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title><![CDATA[MISRA Discussion Forums - 6.11 Member access control (C++)]]></title>
		<link>https://forum.misra.org.uk/</link>
		<description><![CDATA[MISRA Discussion Forums - https://forum.misra.org.uk]]></description>
		<pubDate>Thu, 05 Nov 2026 15:13:52 +0000</pubDate>
		<generator>MyBB</generator>
		<item>
			<title><![CDATA[Rule 11-0-1 and POD types]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1569</link>
			<pubDate>Wed, 24 Mar 2021 13:57:44 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=5614">cgpzs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1569</guid>
			<description><![CDATA[Hi,<br />
<br />
I have a few questions about rule M11-0-1:<br />
<br />
Rule 11–0–1<br />
(Required)<br />
Member data in non-POD class types shall be private.<br />
<br />
* As posted in a previous question, "class types" here means "class, struct or union". Is my understanding correct?<br />
* Why should the fact that a type is POD or not influence access control? <br />
<br />
Let's have the following controversial example. Having the following POD type:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{&nbsp;&nbsp;<br />
&nbsp;&nbsp; int x;<br />
&nbsp;&nbsp; int y;<br />
};</code></div></div><br />
The design for this struct is to aggregate 2 variables together, but there's no invariance to hold. x and y can vary independently. The struct is POD, so it's compliant with M11-0-1.<br />
<br />
Now, in the future we want to extend `Foo` with another variable independent from x and y:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int y;<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::string name;<br />
};</code></div></div><br />
Now, since `Foo` contains a `std:<img src="https://forum.misra.org.uk/images/smilies/confused.png" alt="Confused" title="Confused" class="smilie smilie_13" />tring`, and `std:<img src="https://forum.misra.org.uk/images/smilies/confused.png" alt="Confused" title="Confused" class="smilie smilie_13" />tring` is not a POD, then `Foo` becomes a non-POD. In turn, this now violates M11-0-1, which forces us to make these fields private. This leads to adding trivial boilerplate getters and setters to `Foo`, and every consumer of this struct needs to change their way of interacting with `Foo`.<br />
<br />
This wouldn't happen if `name` was a `char const*`:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int y;<br />
&nbsp;&nbsp;&nbsp;&nbsp;char const* name;<br />
};</code></div></div><br />
The semantics of `Foo` are identical to before, just using a different type for one of its members. Why should that lead to such dramatic changes? <br />
<br />
A similar example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Bar<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::vector points_x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::vector points_y;<br />
};</code></div></div><br />
`Bar` is a collection of points that are independent from each other; there's no invariant. <br />
<br />
Besides, POD types are types that are "compatible with the types used in the C programming language". Why should this be a concern if we are programming in C++? Why should we make our structs "C-compatible", if they are meant to be used in C++ code?]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
I have a few questions about rule M11-0-1:<br />
<br />
Rule 11–0–1<br />
(Required)<br />
Member data in non-POD class types shall be private.<br />
<br />
* As posted in a previous question, "class types" here means "class, struct or union". Is my understanding correct?<br />
* Why should the fact that a type is POD or not influence access control? <br />
<br />
Let's have the following controversial example. Having the following POD type:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{&nbsp;&nbsp;<br />
&nbsp;&nbsp; int x;<br />
&nbsp;&nbsp; int y;<br />
};</code></div></div><br />
The design for this struct is to aggregate 2 variables together, but there's no invariance to hold. x and y can vary independently. The struct is POD, so it's compliant with M11-0-1.<br />
<br />
Now, in the future we want to extend `Foo` with another variable independent from x and y:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int y;<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::string name;<br />
};</code></div></div><br />
Now, since `Foo` contains a `std:<img src="https://forum.misra.org.uk/images/smilies/confused.png" alt="Confused" title="Confused" class="smilie smilie_13" />tring`, and `std:<img src="https://forum.misra.org.uk/images/smilies/confused.png" alt="Confused" title="Confused" class="smilie smilie_13" />tring` is not a POD, then `Foo` becomes a non-POD. In turn, this now violates M11-0-1, which forces us to make these fields private. This leads to adding trivial boilerplate getters and setters to `Foo`, and every consumer of this struct needs to change their way of interacting with `Foo`.<br />
<br />
This wouldn't happen if `name` was a `char const*`:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Foo<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;int x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;int y;<br />
&nbsp;&nbsp;&nbsp;&nbsp;char const* name;<br />
};</code></div></div><br />
The semantics of `Foo` are identical to before, just using a different type for one of its members. Why should that lead to such dramatic changes? <br />
<br />
A similar example:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>struct Bar<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::vector points_x;<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::vector points_y;<br />
};</code></div></div><br />
`Bar` is a collection of points that are independent from each other; there's no invariant. <br />
<br />
Besides, POD types are types that are "compatible with the types used in the C programming language". Why should this be a concern if we are programming in C++? Why should we make our structs "C-compatible", if they are meant to be used in C++ code?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Rule 11-0-1 and protected members]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1355</link>
			<pubDate>Mon, 03 Jul 2017 14:42:21 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4793">tw39124</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1355</guid>
			<description><![CDATA[Rule 11-0-1 says that "Member data in non-POD class types shall be private" and the example clarifies that both "public" and "protected" members violate this rule. The rationale says "the implementation retains more control over how the object state can be modified".<br />
<br />
I fail to see how protected members diminish the "control" over the object state that the implementation has. Can someone explain in more detail why protected member variables specifically are disallowed? A subtype has an "is-a" relationship with the base type, after all, so I don't see why it shouldn't benefit from inheriting the base type's member variables.]]></description>
			<content:encoded><![CDATA[Rule 11-0-1 says that "Member data in non-POD class types shall be private" and the example clarifies that both "public" and "protected" members violate this rule. The rationale says "the implementation retains more control over how the object state can be modified".<br />
<br />
I fail to see how protected members diminish the "control" over the object state that the implementation has. Can someone explain in more detail why protected member variables specifically are disallowed? A subtype has an "is-a" relationship with the base type, after all, so I don't see why it shouldn't benefit from inheriting the base type's member variables.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Clarification for rule 11-0-1]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1295</link>
			<pubDate>Thu, 01 Dec 2016 13:40:58 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=4389">dg1980</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1295</guid>
			<description><![CDATA[Hi,<br />
<br />
the rule text only mentions classes, not structs.<br />
Now, in C++ struct and class are synonyms, except that by default everything is public in a struct.<br />
So, does this rule implicitly apply to all structs as well?<br />
Thank you.]]></description>
			<content:encoded><![CDATA[Hi,<br />
<br />
the rule text only mentions classes, not structs.<br />
Now, in C++ struct and class are synonyms, except that by default everything is public in a struct.<br />
So, does this rule implicitly apply to all structs as well?<br />
Thank you.]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[11.0.1 and static data members]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=1263</link>
			<pubDate>Fri, 08 Jul 2016 15:46:04 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=3331">grunwald</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=1263</guid>
			<description><![CDATA[Does 11.0.1 "Member data in non-POD class types shall be private." apply to all data members, or only non-static data members?<br />
<br />
In particular, I'm interested whether the constant in this code violates 11.0.1:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class MyClass {<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::string non_pod_class;<br />
public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;static const int CONSTANT = 10;<br />
};<br />
<br />
const int MyClass::CONSTANT;</code></div></div>]]></description>
			<content:encoded><![CDATA[Does 11.0.1 "Member data in non-POD class types shall be private." apply to all data members, or only non-static data members?<br />
<br />
In particular, I'm interested whether the constant in this code violates 11.0.1:<br />
<br />
<div class="codeblock"><div class="title">Code:</div><div class="body" dir="ltr"><code>class MyClass {<br />
&nbsp;&nbsp;&nbsp;&nbsp;std::string non_pod_class;<br />
public:<br />
&nbsp;&nbsp;&nbsp;&nbsp;static const int CONSTANT = 10;<br />
};<br />
<br />
const int MyClass::CONSTANT;</code></div></div>]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Use of STL, friend, reinterpret_cast etc..]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=653</link>
			<pubDate>Thu, 07 May 2009 17:18:27 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=201">vijayendra</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=653</guid>
			<description><![CDATA[MISRA C++ Guidelines does not clearly mention about the following C++ constructs:<br />
1. Use of STL (cstring, vector etc) which are internally based on dynamic allocations<br />
2. Use of friend keyword to access private members of another class<br />
3. Use of reinterpret_cast<br />
4. Use of struct rather than classes - in case the set of data does not have specific operations on it, hence no need to encapsulate them in a class<br />
5. Casting from struct to byte pointer - usually needed to send set of data like array of struct<br />
6. Use of string functions which are not unbounded, e.g. strncpy, memcpy, bcopy, memset etc. All these functions accept size as an argument and access only the given area in memory.<br />
<br />
Can you pl. clarify the guidelines on the above?]]></description>
			<content:encoded><![CDATA[MISRA C++ Guidelines does not clearly mention about the following C++ constructs:<br />
1. Use of STL (cstring, vector etc) which are internally based on dynamic allocations<br />
2. Use of friend keyword to access private members of another class<br />
3. Use of reinterpret_cast<br />
4. Use of struct rather than classes - in case the set of data does not have specific operations on it, hence no need to encapsulate them in a class<br />
5. Casting from struct to byte pointer - usually needed to send set of data like array of struct<br />
6. Use of string functions which are not unbounded, e.g. strncpy, memcpy, bcopy, memset etc. All these functions accept size as an argument and access only the given area in memory.<br />
<br />
Can you pl. clarify the guidelines on the above?]]></content:encoded>
		</item>
		<item>
			<title><![CDATA[Can I get by with a little help from My Friends?]]></title>
			<link>https://forum.misra.org.uk/showthread.php?tid=582</link>
			<pubDate>Fri, 12 Sep 2008 18:50:36 +0000</pubDate>
			<dc:creator><![CDATA[<a href="https://forum.misra.org.uk/member.php?action=profile&uid=64">gs</a>]]></dc:creator>
			<guid isPermaLink="false">https://forum.misra.org.uk/showthread.php?tid=582</guid>
			<description><![CDATA[Rule 11-0-1 states,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>Member data in non-POD class types shall be private.</blockquote>
with the rationale:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>By implementing class interfaces with member functions, the implementation retains more control over how the object state can be modified, and helps to allow a class to be maintained without affecting clients.</blockquote>
Does the above mean non-POD structure types should not contain any friend declarations?  A friend declaration would <span style="font-style: italic;" class="mycode_i">by definition</span> allow for the circumvention of the access restrictions.]]></description>
			<content:encoded><![CDATA[Rule 11-0-1 states,<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>Member data in non-POD class types shall be private.</blockquote>
with the rationale:<br />
<blockquote class="mycode_quote"><cite>Quote:</cite>By implementing class interfaces with member functions, the implementation retains more control over how the object state can be modified, and helps to allow a class to be maintained without affecting clients.</blockquote>
Does the above mean non-POD structure types should not contain any friend declarations?  A friend declaration would <span style="font-style: italic;" class="mycode_i">by definition</span> allow for the circumvention of the access restrictions.]]></content:encoded>
		</item>
	</channel>
</rss>