开发者

'enclosing' scopes for friendship classes

Bjarne Stroustrup writes:开发者_StackOverflow

"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?


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.


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.


Friends don't let friends use friends.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜