Topic: order of friend declarations?


Author: roger@PROCASE.COM (Roger H. Scott)
Date: 10 Feb 95 03:50:21 GMT
Raw View
Is the position at which a friend declaration occurs within a class supposed
to matter?  Consider the following example:

class Foo {
    static int privateMember;

    class NestedBefore {
 void in_line() {
     Foo::privateMember = 0; // case 1
 }
 void out_of_line(); // case 2 (see below)
    };
    friend class NestedBefore;

    class NestedAfter; // anyone care to explain why this is needed? [it *is*]
    friend class NestedAfter;
    class NestedAfter {
 void f() {
     Foo::privateMember = 0; // case 3
 }
    };
};

void Foo::NestedBefore::out_of_line() {
    Foo::privateMember = 0;
}


Which of these three cases are supposed to be legal?  Current compilers seem
to feel that (1) is illegal, but that seems bogus - especially in light of (2).
Having to resort to the weirdness for (3) seems a bit extreme.

Is this issue addressed adequately in the standard?




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 10 Feb 1995 17:23:04 GMT
Raw View
In article 999@heaven.UUCP, roger@PROCASE.COM (Roger H. Scott) writes:
>Is the position at which a friend declaration occurs within a class supposed
>to matter?

A friend declaration is not a forward declaration within the class. If
it declares a new name, that name is injected into the enclosing scope,
and is not assumed to be a forward declaration in the same scope. If that
name is already visible, the friend declaration refers to the visible name.

>Consider the following example:
>
>class Foo {
>    static int privateMember;
>
>    class NestedBefore {
> void in_line() {
>     Foo::privateMember = 0; // case 1
> }
> void out_of_line(); // case 2 (see below)
>    };
>    friend class NestedBefore;
>
>    class NestedAfter; // anyone care to explain why this is needed? [it *is*]

If you omit the above line, the line which follows declares a hypothetical
file-scope class 'NestedAfter' to be a friend. It says nothing about the
local class 'NestedAfter'. The above line is a forward declaration for the
local class, and thus is needed.

>    friend class NestedAfter;
>    class NestedAfter {
> void f() {
>     Foo::privateMember = 0; // case 3
> }
>    };
>};

---
Steve Clamage, stephen.clamage@eng.sun.com