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


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

Re: Does friendship extend to inner classes?

From Kai-Uwe Bux <jkherciueh@gmx.net>
Newsgroups comp.lang.c++
Subject Re: Does friendship extend to inner classes?
Followup-To comp.lang.c++
Date 2011-06-12 23:01 +0200
Organization University of Bielefeld
Message-ID <it39ra$m54$1@hoshi.visyn.net> (permalink)
References <4df25dc2$0$2869$7b1e8fa0@news.nbl.fi>

Followups directed to: comp.lang.c++

Show all headers | View raw


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.


Best,

Kai-Uwe Bux

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