Search code examples
c++classfriend

Definition of friend functions and classes


Why friend functions can be defined

class CL
{
    friend void fnc(){}
};

but friend classes can't?

class CL
{
    friend class FriendClass{}; //error
};

Solution

  • An inner class is a friend of the class it is declared within.

    It would have no meaning to do it with a friend specifier since it would be an inner class with private-access to the outer one.

    And, formally, the standard says:

    [class.friend]/p2

    A class shall not be defined in a friend declaration