Topic: local classes as friends?


Author: "Gabor Greif" <gabor@datawatch.de>
Date: 1997/10/30
Raw View
On Wed, Oct 29, 1997 3:19 PM, Chuck McCorvey
<mailto:chuckm@solutionware.com> wrote:
>Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote in article
[snip]
> >
> > The reason for this is that the friend declaration refers to `::local',
> > not `A::local'.
> >
> > To make it work, you need to insert `struct local;' before the friend
> > declaration, e.g.
> >
> >  struct A {
> >   struct local;
> >   friend struct local;
> >   ...
> >  };
>
>This is also incorrect.  His struct local is defined within the member
>function and is thus not the same struct local that you are trying to
>befriend.  There is no way to make the struct local inside foo() a friend
>of the class.  You will have to define the struct outside of the function.

It is obvious that it will work that way. But it does not answer my
original question: Is it possible to declare a local class to be friend
of an other class?

It is just a gut feeling that this is a little hole in the standard.

If there is no way to do it, the utility of local classes is severely
limited...  i.e. one cannot use them in member functions, if access to
private variables is needed. It seems to be inconsistent to me that a
member function can access private variables, but a local class defined
in that member function must not.  This is counterintuitive, and
hinders structuring of code. Since in C++ member functions should be
the rule and not the exception, the problem is especially serious.

Of course very few people use local classes anyway :-( so this will be a
non-issue.

Anyway, I would reassert that a local class should inherit every
friendship privilege from its enclosing function.

 Gabor
---
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/10/28
Raw View
"Gabor Greif" <gabor@datawatch.de> writes:

 >Recently I tried following piece of code
 >
 >struct A
 >{
 > private: int i;
 > friend struct local;
 >
 > void foo(void)
 > {
 >  struct local
 >  {
 >   A& b;
 >   int  i2;
 >   local(A& _b) : b(_b), i2(_b.i) { } // illegal access to private: _b.i
 >  } n(*this);
 > }
 >
 >};
 >
 >It did not compile because of an illegal access to private member.

The reason for this is that the friend declaration refers to `::local',
not `A::local'.

To make it work, you need to insert `struct local;' before the friend
declaration, e.g.

 struct A {
  struct local;
  friend struct local;
  ...
 };

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ 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: "Chuck McCorvey" <chuckm@solutionware.com>
Date: 1997/10/29
Raw View
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote in article
<634q61$7mg@mulga.cs.mu.OZ.AU>...
 > "Gabor Greif" <gabor@datawatch.de> writes:
 >
 >  >Recently I tried following piece of code
 >  >
 >  >struct A
 >  >{
 >  > private: int i;
 >  > friend struct local;
 >  >
 >  > void foo(void)
 >  > {
 >  >  struct local
 >  >  {
 >  >   A& b;
 >  >   int  i2;
 >  >   local(A& _b) : b(_b), i2(_b.i) { } // illegal access to private:
_b.i
 >  >  } n(*this);
 >  > }
 >  >
 >  >};
 >  >
 >  >It did not compile because of an illegal access to private member.
 >
 > The reason for this is that the friend declaration refers to `::local',
 > not `A::local'.
 >
 > To make it work, you need to insert `struct local;' before the friend
 > declaration, e.g.
 >
 >  struct A {
 >   struct local;
 >   friend struct local;
 >   ...
 >  };

This is also incorrect.  His struct local is defined within the member
function and is thus not the same struct local that you are trying to
befriend.  There is no way to make the struct local inside foo() a friend
of the class.  You will have to define the struct outside of the function.
---
[ 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: "Gabor Greif" <gabor@datawatch.de>
Date: 1997/10/28
Raw View
Recently I tried following piece of code

struct A
{
 private: int i;
 friend struct local;

 void foo(void)
 {
  struct local
  {
   A& b;
   int  i2;
   local(A& _b) : b(_b), i2(_b.i) { } // illegal access to private: _b.i
  } n(*this);
 }

};

It did not compile because of an illegal access to private member.
The Dec 96 WP was no help, I did not find the way how to declare
a local class as a friend of a class.

The syntax above is what I would think of, but there could be another
interpretation: Local classes should inherit the friendship privileges
from their enclosing functions.

Either way the code above should compile...

Am I considering everything here?

 Gabor
---
[ 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                             ]