Topic: Does local class have access to private names?


Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1996/10/25
Raw View
John Aldridge writes:

> Should the following example compile?

I don't think so.

> class A {
> public:
>    void B ();
> private:
>    enum X {X1, X2, X3};
> };

> void A::B ()
> {
>    struct Z {X x; int i;};
> }

> Specifically, is the type name X accessible from within the local
> class Z?  The compilers available to me disagree (VMS, Sun say
> no; HP, MSVC & g++ say yes), and I can't find anything in the
> draft standard which makes the intention clear.

As nested classes do not have  any access priviledges, I believe local
classes do not either.  It would be nice to clarify this in the DWP.

Anyway, you can make  type X accessible to struct  Z by adding, before
Z's declaration:

  typedef X <somename>;

then using <somename> in  Z, instead of X.   I have tested this in g++
and it  worked, but I can't assure  it will work with  other compilers
because  g++ still  allows  access  in  some situations it  shouldn't.
Anyway, my reading of the standard says this should work.


--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: John Aldridge <jpsa@uk.gdscorp.com>
Date: 1996/10/28
Raw View
I wrote:

> Should the following example compile?
>
> class A {
> public:
>    void B ();
> private:
>    enum X {X1, X2, X3};
> };
>
> void A::B ()
> {
>    struct Z {X x; int i;};
> }

The general opinion seems to be that the answer should be "no", but that
the DWP is not entirely clear on the subject.  If Z were a nested class
the answer would be clear because under "Nested class declarations", the
DWP contains the paragraph:

   The scope of a nested class has no special access to members
   of an enclosing class; the usual access rules (_class.access_)
   shall be obeyed.

There is no equivalent paragraph under "Local class declarations", which
lead me to believe that it might be intended that local classes should
have the access rights of their enclosing scope.

Cheers,
John Aldridge
jpsa@uk.gdscorp.com


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: John Aldridge <jpsa@uk.gdscorp.com>
Date: 1996/10/23
Raw View
Should the following example compile?

class A {
public:
   void B ();
private:
   enum X {X1, X2, X3};
};

void A::B ()
{
   struct Z {X x; int i;};
}

Specifically, is the type name X accessible from within the local
class Z?  The compilers available to me disagree (VMS, Sun say
no; HP, MSVC & g++ say yes), and I can't find anything in the
draft standard which makes the intention clear.

Cheers,
John Aldridge
jpsa@uk.gdscorp.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: paul.black@vf.vodafone.co.uk
Date: 1996/10/24
Raw View
John Aldridge <jpsa@uk.gdscorp.com> wrote:
> Should the following example compile?
>
> class A {
> public:
>    void B ();
> private:
>    enum X {X1, X2, X3};
> };
>
> void A::B ()
> {
>    struct Z {X x; int i;};
> }
>
> Specifically, is the type name X accessible from within the local
> class Z?  The compilers available to me disagree (VMS, Sun say
> no; HP, MSVC & g++ say yes), and I can't find anything in the
> draft standard which makes the intention clear.
>
> Cheers,
> John Aldridge
> jpsa@uk.gdscorp.com

It shouldn't compile. In the April 1995 draft, Section 11 (Member Access Control
- [class.access]), the 3rd paragraph states that access control is applied
uniformly to all names. A::X is private therefore inaccessible.

Paul
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]