union protected access

  • Thread starter Martin Vorbrodt
  • Start date
M

Martin Vorbrodt

say i have this:

union U {
public:
U(int a) : x(a) {}
protected:
int x, y, z;
};

why is the protected access even allowed for unions? no inheritance can take
place anyways, why not just have public and private only?

thanx,
martin
 
D

Derrick Coetzee

Martin said:
why is the protected access even allowed for unions? no inheritance can take
place anyways, why not just have public and private only?

Even just having public and private in unions is rather silly, since the
presence of even one public field exposes all the others to mutation,
breaking encapsulation. Such specifiers in unions are an artifact of the
standard's attempt to unify class, struct, and union as all being forms
of an abstract object description concept, and giving them parallel
syntaxes. One could say this simplifies compiler implementations, but
this is dubious.

Were I designing it, I think I would allow only one of two choices:
1. All members are public; the C-style union
2. All members are private; handy for constructing tagged unions with
computed tags
 
P

Paul

Derrick Coetzee said:
Even just having public and private in unions is rather silly, since the
presence of even one public field exposes all the others to mutation,
breaking encapsulation. Such specifiers in unions are an artifact of the
standard's attempt to unify class, struct, and union as all being forms
of an abstract object description concept, and giving them parallel
syntaxes. One could say this simplifies compiler implementations, but
this is dubious.

Were I designing it, I think I would allow only one of two choices:
1. All members are public; the C-style union
2. All members are private; handy for constructing tagged unions with
computed tags

consider this code...
class A
{
public:
A(){}
A(int x, int y)
{
_pubX = x;
_priX = y;
}
int _pubX;
private:
int _priX;
};

int main()
{
A a(10,50);
int* p = (int*)&a;
printf("%d %d\n",*p, *(p+1));
return 0;
}

this way we can still access private, does this mean public, private,
protected are useless in C++. Then it raises a question, what does
encapsulation means, restrcting memory or member access? we can't
restrict memory access though, then...
appreciate if you can provide your inputs?

Br.
-Paul.
 
A

Andre Kostur

(e-mail address removed) (Paul) wrote in

this way we can still access private, does this mean public, private,
protected are useless in C++. Then it raises a question, what does
encapsulation means, restrcting memory or member access? we can't
restrict memory access though, then...
appreciate if you can provide your inputs?

Public, protected, and private are not intended to protect against
maliciousness. Besides, there's an easier way:

#define private public
#define protected public

#include "someclass.h"
 
D

Derrick Coetzee

Paul said:
Derrick Coetzee said:
Even just having public and private in unions is rather silly, since the
presence of even one public field exposes all the others to mutation,
breaking encapsulation.

[ . . . ] we can still access private, does this mean public, private,
protected are useless in C++.

The point of protected and private are not to make it impossible for
someone to access the data, but to make it difficult enough that they
aren't likely to do it accidentally. In a union, it's easy to access the
data as long as a public member is visible, so easy that it could be
done accidentally and cause errors. This is what I mean by breaking
encapsulation in a C++ context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,495
Latest member
Jack William

Latest Threads

Top