Search code examples
c++classfriendprotectedprivate-members

Placement of friend declarations


Does it matter where in a class a friend clause is placed (i.e. within the protected block as opposed to the private block)?


Solution

  • No it does not.

    class X
    {
    public:
        friend class A;
    private:
        friend class B;
    protected:
        friend class C;
    };
    

    All three classes are now friends of X and share the exact same priviliges.

    A good convention is to group all friend declarations together for visibility, but that's just style.

    11.4 Friends

    9) A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration. The meaning of the friend declaration is the same whether the friend declaration appears in the private, protected or public (9.2) portion of the class member-specification.