Topic: friend of private nested class - is this legal C++?
Author: "Eigil Hysv r" <eigil.hysvaer@eudoramail.com>
Date: Fri, 31 May 2002 23:58:35 GMT Raw View
Joseph Heled <joseph@itgssi.com> wrote in
news:3CF277C1.B98D3211@itgssi.com:
>
> Is this legal or not? g++ 3.1 says not, but I can't see why.
> I would appreciate a comment from a "language lawyer" ...
>
> ----------------------------------------------
> class A {
> private:
> class B {
> friend void f(B&);
> };
> };
>
> void
> f(A::B&) {}
> ----------------------------------------------
>
> =================================================
> g++ -Wall -c t3.cc
> t3.cc: In function `void f(A::B&)':
> t3.cc:4: `class A::B' is private
> t3.cc:11: within this context
> =================================================
>
Both your classes is legal code. Since B is private, only A is able to create
an object of B and thus is the only one able to call 'f(A::B&)'. It looks
like you're calling 'f' from the global scope in your example - how did it
get access to a 'B'-object?
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Steve Clamage <clamage@eng.sun.com>
Date: Wed, 29 May 2002 15:57:41 GMT Raw View
On Mon, 27 May 2002, Joseph Heled wrote:
>
> class A {
> private:
> class B {
> friend void f(B&);
> };
> };
>
> void
> f(A::B&) {}
>
> =================================================
> g++ -Wall -c t3.cc
> t3.cc: In function `void f(A::B&)':
> t3.cc:4: `class A::B' is private
> t3.cc:11: within this context
> =================================================
>
> Artur Siekielski claimed,
>
> This declaration says, that 'void f(B&)' has access to private and protected
> members of 'B' class, it says anything about accessing objects of type 'B'.
>
> And suggested I would repeat the friendship relation inside A.
That is correct, under current language rules.
But a proposal is before the C++ Committee that would give member
classes full access to the class itself. Under that modifed language
rule, the code would be valid. I believe the proposal will be adopted
in a Technical Corrigendum to the standard, but it will not be in the
TC which is currently in progress.
--
Steve Clamage, stephen.clamage@sun.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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Joseph Heled <joseph@itgssi.com>
Date: Mon, 27 May 2002 19:19:51 GMT Raw View
Is this legal or not? g++ 3.1 says not, but I can't see why.
I would appreciate a comment from a "language lawyer" ...
----------------------------------------------
class A {
private:
class B {
friend void f(B&);
};
};
void
f(A::B&) {}
----------------------------------------------
=================================================
g++ -Wall -c t3.cc
t3.cc: In function `void f(A::B&)':
t3.cc:4: `class A::B' is private
t3.cc:11: within this context
=================================================
Artur Siekielski claimed,
This declaration says, that 'void f(B&)' has access to private and protected
members of 'B' class, it says anything about accessing objects of type 'B'.
And suggested I would repeat the friendship relation inside A.
I would think it ironic if 'f' can access protected/private parts of B, but
can't access a B itself.
Of course I was trying to define operator<< (i.e. 'friend operator<<(ostream&,
B&)'), and worse, his suggestion fails in my case, since I am using forwarded
nested classes.
class A {
private:
class B;
// *CAN'T* put 'friend void f(A::B::C&);' here, since B body is not know yet.
};
class A::B {
private:
class C;
// I can put 'friend void f(C&);' here
};
class A::B::C {
public:
friend void f(C&);
};
I think my example shows that if this is the standard, perhaps it is a slightly
broken? I would expect the granting of friendship to automatically grant access
to the class itself.
Thanks. Joseph
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]