The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears. Friends have access to all members of a class.
A class can declare friendship to a function, a method, or a class.
- The declaration of a friendship can be anywhere.
- The access specifier of the friendship declaration is not relevant.
- Friendship cannot be inherited (your friend’s children are not your friends).
- Friendship is not transitive (a friend of your friend is not your friend).
Access specifiers have no effect on the meaning of friend declarations (they can appear in
private: or in
public: sections, with no differences).
A friend has full control of the internals of a class.