Article View: comp.std.c++
Article #1829Re: Scoping and friends
From: Steve Clamage
Date: Wed, 23 Aug 1995 22:27
Date: Wed, 23 Aug 1995 22:27
67 lines
2497 bytes
2497 bytes
In article 95Aug23144855@oeltemp1.jpl.nasa.gov, maxwell@oeltemp1.jpl.nasa.gov (Scott Maxwell) writes:
>
>When a friend function of a private (or protected) nested class tries
>to use the type name of the class it's a friend of, I get errors under
>Borland C++ 4.5 but not under g++ 2.6.3. Who's right?
Borland, although you also have another error.
>class Outer
>{
> class Inner
> {
Notice that class Inner is private in class Outer, and thus cannot be
accessed at file scope. (Cfront did not check this access, so some compilers
allow the access as an option to allow old code to compile.)
> int x;
>
> public:
> Inner(void): x(0) {}
> friend void pal(Inner &); // Friend fn that modifies an Inner.
Function pal is an ordinary file-scope function.
> static void smf(Inner &); // Static mfn that does the same thing.
> } i1, i2;
>
>public:
> Outer(void) {}
> void blah(void)
> {
> smf(i1); smf(i2);
The above line should not compile, since the name 'smf' is not known in
the current scope. You need to say "Inner::smf".
> pal(i1); pal(i2);
> }
>};
>
>void Outer::Inner::smf(Outer::Inner & i) // No problem.
>{
> i.x++;
>}
>
>void pal(Outer::Inner & i) // BC++ error: "Outer::Inner is not accessible"!
>{
> i.x++;
>}
BC++ is correct. "pal" is at file scope, and "Outer::Inner" is not
accessible from there. Even if you could compile this code, you can't
call 'pal' from any scope outside a friend or member of Outer, since
no object of type Outer::Inner could be declared anywhere else.
Either make Inner a public type, or put "pal" within the scope of Outer
somehow, as "smf" is.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<41ga04$psk@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Scoping and friends Date: 23 Aug 1995 22:27:16 GMT Organization: Sun Microsystems Inc., Mountain View, CA Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <41ga04$psk@engnews2.Eng.Sun.COM> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com Content-Length: 1720 X-Lines: 63 Originator: clamage@taumet