Topic: Nested classes in C++


Author: jbn@lulea.telesoft.se (Johan Bengtsson)
Date: 25 Jul 91 14:11:56 GMT
Raw View
> It seems C++ as defined in ARM has elected to have nested types follow the scope
> resolution but not the access rules that apply to data.

No, this is not correct.

ARM p239:
 "Access control applies uniformly to function members,
  data members, member constants and nested types."

>
> class X {
> private:
>  struct Y { /* ... */ }
> };
>
> X::Y b;   // legal C++

Not legal. X::Y is private.

>
> Why is the type Y not hidden in the private section of X as one might expect?

Allow me to clarify that X::Y is not *hidden*, but should be
*inaccessible* in this case.  Anyway, at least Sun C++ 2.0 wrongly
makes all nested classes public.  So why is this?  Well we have

ARM p187:
 "Member functions of nested classes have no special access
  to members of an object of the enclosing class; they obey
  the usual access rules."

Note that if X::Y is made private, then the member functions of
X::Y would not have access to the name X::Y, because it is private,
making it almost impossible do anything useful in X::Y!

Conclusion:
 If the rules of ARM are strictly followed, then nested classes
 must be declared public.

Proposal:
 Give nested classes full access to the enclosing class. This
 makes it possible to declare protected and private nested classes.

Redirecting to comp.std.c++ as well.


-- Johan

-----------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden     |
| Email: jbn@lulea.telesoft.se; Voice: (+46) 92075471; Fax: (+46) 92075490  |
-----------------------------------------------------------------------------
--
-----------------------------------------------------------------------------
| Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden     |
| Email: jbn@lulea.telesoft.se; Voice: (+46) 92075471; Fax: (+46) 92075490  |
-----------------------------------------------------------------------------