Thread View: comp.std.c++
1 messages
1 total messages
Started by Steve Clamage
Wed, 23 Aug 1995 22:27
Re: Scoping and friends
Author: 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. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads