Namespaces
Variants
Views
Actions

cpp/language/friend

From cppreference.com
< cpp‎ | language
Revision as of 06:15, 15 October 2011 by P12 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

The friend keyword is used to grant access to class members from outside of the class. For example, suppose your class has private or protected members. You can access these members only from the same class or derived classes respectively. By using the friend keyword you grant access to the private and protected members of your class to the friend function or a friend class. Some of the more common uses of friend are the operator>> and operator<< functions and the swap function.


Example use

friend ostream& operator<<(ostream& out, const T& o);
friend istream& operator>>(istream& in, T& o);
friend swap(T& left, T& right);

References

You can find more information in the C++ FAQ.