Re: union protected access
From: Paul (bgaloma_at_yahoo.com)
Date: 09/02/04
- Next message: Peter Koch Larsen: "Re: Why not develop new language"
- Previous message: Alf P. Steinbach: "Re: calling base class function only once"
- In reply to: Derrick Coetzee: "Re: union protected access"
- Next in thread: Andre Kostur: "Re: union protected access"
- Reply: Andre Kostur: "Re: union protected access"
- Reply: Derrick Coetzee: "Re: union protected access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 2 Sep 2004 02:08:10 -0700
Derrick Coetzee <dcnews@moonflare.com> wrote in message news:<ch5rr6$qn4$1@news-int2.gatech.edu>...
> Martin Vorbrodt wrote:
>
> > 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
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.
- Next message: Peter Koch Larsen: "Re: Why not develop new language"
- Previous message: Alf P. Steinbach: "Re: calling base class function only once"
- In reply to: Derrick Coetzee: "Re: union protected access"
- Next in thread: Andre Kostur: "Re: union protected access"
- Reply: Andre Kostur: "Re: union protected access"
- Reply: Derrick Coetzee: "Re: union protected access"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|