tags:

views:

102

answers:

3

Bjarne Stroustrup writes:

"a friend class must be previously declared in an enclosing scope or defined in the non-class scope immediately enclosing the class that is declaring it a friend"

Isn't the first part of the statement redundant, because the "non-class scope immediately enclosing the class " includes "previously declared in an enclosing scope"? IE:, what is the difference in these two scopes?

+1  A: 

I think in the first part, the friend class is declared inside a class (nested) that declares it a friend. While in the second part, the friend class is declared outside, just before the class that declared it a friend.

jasonline
+1  A: 

Perhaps that the second part says "immediately enclosing" and the first one does not? So I gather from it that if the class is declared in a non-class scope (i.e. namespace or global), it can be only the immediately enclosing scope (one up). If it's declared in an enclosing class scope (think nested classes), it can be as much above as you want.

Meaning the following is valid:

class Grandpa
{
    class Mother
    {
        class Junior
        {
            friend class Grandpa;
        };

    };
};

Just because Grandpa is several "class scopes" above Junior.

Eli Bendersky
I think you might be onto something but it's hard to make sure given that the language is (inherently) imprecise. The bit that threw me off was "a friend class must be previously declared in an enclosing scope ". If you declare (which AFAIK is the same as define when talking about classes) a friend class in the global scope (which encloses *all* scopes), it doesn't work.
Taras
alas, English is a much more ambiguous language than c++ :-)
Eli Bendersky
A: 

Friends don't let friends use friends.

Charles Eli Cheese
Rather, friends have access to your private members, which is something you generally don't want.
greyfade
It's funny because it's true.
Charles Eli Cheese