Re: union protected access

From: Paul (bgaloma_at_yahoo.com)
Date: 09/02/04


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.



Relevant Pages

  • Re: Why Simitis tried so hard?
    ... in the uk the unions aimed at installing a socialist state. ... Again, if it is institutionalized, it is not corruption!! ... this is what constituted the tipping point for Greece. ... there are big private hospitals. ...
    (soc.culture.greek)
  • Stop the Employee NO Choice Act!
    ... Private ballots are the foundation of our democracy. ... unions have respected this right when organizing workers. ... work and at home by organizers intent on obtaining signatures. ...
    (soc.retirement)
  • Re: Union Sundown
    ... unions' fundamental power to bargain for wages. ... "They insist this is the end of unionization in government, ... Unions in the private sector are ... private interest, that of employees, against the public's interest, ...
    (rec.music.dylan)
  • Re: OT Did you get your open season insurance docs yet?
    ... Private industry, which private insurance companies are part of, ... Big cities are dominated by government unions. ... In this case we are talking about government unions. ...
    (rec.boats)
  • Re: union protected access
    ... >>Even just having public and private in unions is rather silly, ... >>presence of even one public field exposes all the others to mutation, ... >>breaking encapsulation. ...
    (comp.lang.cpp)