C++ Reference ยท Keywords and Statements
The friend declaration
Reference for C++ friend: granting functions and classes access to private members, hidden friends for operators, friend templates and common pitfalls.
A friend declaration inside a class grants a function, a member function of another class, or a whole class the right to access that class's private and protected members. Friendship is not a member: it is a permission slip written by the class that is giving up some encapsulation.
The most common modern use is the hidden friend: a non-member operator defined inside the class body. It has full access to the members, yet is found only through argument-dependent lookup, which keeps it out of the surrounding namespace.
class Money { long cents_ = 0; public: friend bool operator==(const Money& a, const Money& b) { return a.cents_ == b.cents_; } };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
friend declaration-of-function; // friend void f(X&); friend class Name; friend Name; // friend class (C++11 allows the short form) friend ReturnType Other::member(params); // one member function of another class template <class T> friend class Box; // all specializations of a template friend Type; // a typedef, alias or template parameter (C++11)
Usage notes
- Friendship is not mutual (granting A friend access does not give you access to A), not transitive (a friend of a friend gets nothing) and not inherited (friends of a base are not friends of derived classes, and a derived class does not inherit its base's friends' rights).
- The access specifier (
public,private) where thefriendline appears does not matter. - A friend function declared only inside a class is not visible to ordinary lookup; it can be called only via argument-dependent lookup. If you need a normal declaration, add one at namespace scope.
- Defining the function in the class body makes it implicitly
inline. This is the hidden friend idiom, and it is the preferred way to writeoperator<<,operator==,swapand other symmetric operators. frienddoes not weaken access to anything outside the named entities. Use it when two types are designed together (an iterator and its container, a class and its test fixture), not as a shortcut around design problems.- Befriending a template parameter (
friend T;) is allowed since C++11 and is used by policy classes and passkey idioms. - Friends can break invariants just as easily as public members can: keep the friend list short and in the same translation unit when possible.
- A friend function cannot use the
staticor storage-class specifiers on its first declaration, and afrienddeclaration is not a member, so it has nothis.