Topic: accessibility of nested classes by enclosing class' aggregate members


Author: "Jeff Greif" <jmg@spam-me-not.trivida.com>
Date: 2000/03/09
Raw View
Several compilers accept this code:

class XXX {
private:
  struct A {
    int a_;
  };
  struct B {
    double b_;
  };
  union {
    int i_;
    B* bp_;
    A* ap_;
  } value_;
};

XXX xxx;

but one compiler complains about the reference to A* and B* inside the
union:

"junk4.cpp", line 11: Error: XXX::B is not accessible from XXX::union.
"junk4.cpp", line 12: Error: XXX::A is not accessible from XXX::union.

Are these references erroneous, or is the compiler that gripes?  Thanks.

Jeff

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/09
Raw View

Jeff Greif wrote:
>
> Several compilers accept this code:
>
> class XXX {
> private:
>   struct A {
>     int a_;
>   };
>   struct B {
>     double b_;
>   };
>   union {
>     int i_;
>     B* bp_;
>     A* ap_;
>   } value_;
> };
>

>
> Are these references erroneous, or is the compiler that gripes?  Thanks.
>

A and B are private to XXX, they can't be used in another class/union even
if it is defined within XXX.   The standard says pretty clearly:  The members
of a nested class have no special access to members of the enclosing class.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/03/09
Raw View
On Thu, 9 Mar 2000 08:46:08 CST, "Jeff Greif"
<jmg@spam-me-not.trivida.com> wrote:

: Several compilers accept this code:
:
: class XXX {
: private:
:   struct A {
:     int a_;
:   };
:   struct B {
:     double b_;
:   };
:   union {
:     int i_;
:     B* bp_;
:     A* ap_;
:   } value_;
: };
:
: XXX xxx;
:
: but one compiler complains about the reference to A* and B* inside the
: union:
:
: "junk4.cpp", line 11: Error: XXX::B is not accessible from XXX::union.
: "junk4.cpp", line 12: Error: XXX::A is not accessible from XXX::union.
:
: Are these references erroneous, or is the compiler that gripes?  Thanks.

Once upon a time, nested classes were treated like friend.  No matter
where you put them, they were public.  Many compilers still accept it
for backwards compatibility.  Try a strict mode.  Also try a global

XXX::A a;

The compilers that accept the above will likely accept that also.  The
rules are that nested classes have no special rights to the enclosing
class and conversly.  The usual solution is to make the nested class
a friend.  You will have a hard time making the anonymous union a
friend. :)  Give it a name.

friend class Union;
union Union {

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/03/10
Raw View
Ron Natalie <ron@sensor.com> writes:

> A and B are private to XXX, they can't be used in another class/union even
> if it is defined within XXX.   The standard says pretty clearly:  The members
> of a nested class have no special access to members of the enclosing class.

Of course, according to Core Issue 45, this is a defect, and access
should be granted in the example. See

http://www.informatik.hu-berlin.de/~loewis/corer9.html#45

for details.

Martin

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 2000/03/10
Raw View
Ron Natalie wrote:

> Jeff Greif wrote:

> > class XXX {
> > private:
> >   struct A {
> >     int a_;
> >   };
> >   struct B {
> >     double b_;
> >   };
> >   union {
> >     int i_;
> >     B* bp_;
> >     A* ap_;
> >   } value_;
> > };

> A and B are private to XXX, they can't be used in another class/union even
> if it is defined within XXX.

There is a proposed resolution to change that.

--

Valentin Bonnard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]