Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6662

Re: Does friendship extend to inner classes?

From "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Does friendship extend to inner classes?
Date 2011-06-12 23:31 +0200
Organization A noiseless patient Spider
Message-ID <it3b32$fqu$1@dont-email.me> (permalink)
References <4df25dc2$0$2869$7b1e8fa0@news.nbl.fi> <it39ra$m54$1@hoshi.visyn.net>

Show all headers | View raw


* Kai-Uwe Bux, on 12.06.2011 23:01:
> Juha Nieminen wrote:
>
>>    Consider the following:
>>
>> //-----------------------------------------------
>> class A
>> {
>>      class B
>>      {
>>          int i;
>>          friend class A;
>>      };
>>
>>      class C
>>      {
>>          void foo(B&);
>>      };
>> };
>>
>> void A::C::foo(B&  b)
>> {
>>      b.i = 5; // Allowed or not?
>> }
>> //-----------------------------------------------
>>
>>    It seems that gcc and Visual Studio disagree on this (gcc allows it,
>> Visual Studio issues an error). Which one is right?
>
> The C++03 standard [11.4/2]:
>
>    Declaring a class to be a friend implies that the names of private and
>    protected members from the class granting friendship can be accessed in
>    declarations of members of the befriended class. [Note: this means that
>    access to private and protected names is also granted to member functions
>    of the friend class (as if the functions were each friends) and to the
>    static data member definitions of the friend class. This also means that
>    private and protected type names from the class granting friendship can be
>    used in the base-clause of a nested class of the friend class.
>
> and now for the important sentence:
>
>    However, the declarations of members of classes nested within the friend
>    class cannot access the names of private and protected members from the
>    class granting friendship.
>
> There is also an example:
>
>    Also, because the base-clause of the friend class is not part of its
>    member declarations, the base-clause of the friend class cannot access the
>    names of the private and protected members from the class granting
>    friendship. For example,
>        class A {
>                   class B { };
>                   friend class X;
>        };
>        class X : A::B {        // ill-formed: A::B cannot be accessed
>                                // in the base-clause for X
>                   A::B mx;     // OK: A::B used to declare member of X
>                   class Y : A::B {  // OK: A::B used to declare member of X
>                        A::B my;  // ill-formed: A::B cannot be accessed
>                                  // to declare members of nested class of X
>                   };
>        };
>    ]
>    ...
>
>
> This explanation in in a non-normative a note; and one could argue that the
> interpretation that friendship does not extend to inner classes is a little
> bit streched given the wording of the normative provision. C++0X has taken
> that stance. In n3291, the corresponding clause [11.3/2] reads:
>
>    Declaring a class to be a friend implies that the names of private and
>    protected members from the class granting friendship can be accessed in
>    the base-specifiers and member declarations of the befriended class.
>    [ Example:
>     class A {
>       class B { };
>       friend class X;
>     };
>     struct X : A::B {    // OK: A::B accessible to friend
>       A::B mx;           // OK: A::B accessible to member of friend
>       class Y {
>          A::B my;        // OK: A::B accessible to nested member of friend
>       };
>     };
>     -- end example ]
>     ...
>
> As one can see from the example, C++ is changing in this regard. That could
> explain your observation that compilers disagree on this one.

I feel dizzy with all these to me arbitrary changes.

I remember, at meeting of Oslo C++ User Group some years ago the lecturer raised 
a question about visibility for nested class friend thing.

And it turned out the rules had changed already two or three times  --  then.

So I think this is a very practical question:

HOW TO DEAL WITH THE EVER CHANGING RULES?

It is like shooting at a moving target.

And since that is a classical problem, someone must (hopefully) found a solution?


Cheers,

- Alf

-- 
blog at <url: http://alfps.wordpress.com>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Does friendship extend to inner classes? Juha Nieminen <nospam@thanks.invalid> - 2011-06-10 18:09 +0000
  Re: Does friendship extend to inner classes? Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-10 13:04 -0700
    Re: Does friendship extend to inner classes? Juha Nieminen <nospam@thanks.invalid> - 2011-06-11 04:45 +0000
      Re: Does friendship extend to inner classes? Geoff <geoff@invalid.invalid> - 2011-06-12 10:53 -0700
  Re: Does friendship extend to inner classes? Ruben Safir <mrbrklyn@panix.com> - 2011-06-11 20:41 +0000
    Re: Does friendship extend to inner classes? Juha Nieminen <nospam@thanks.invalid> - 2011-06-12 14:10 +0000
    Re: Does friendship extend to inner classes? Werner <werasm@gmail.com> - 2011-06-13 03:51 -0700
  Re: Does friendship extend to inner classes? Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-12 23:01 +0200
    Re: Does friendship extend to inner classes? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-06-12 23:31 +0200
    Re: Does friendship extend to inner classes? Juha Nieminen <nospam@thanks.invalid> - 2011-06-13 12:20 +0000
      Re: Does friendship extend to inner classes? Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-06-13 18:46 +0200

csiph-web