friend specifier

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
friend specifier
initializer lists
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

[edit] 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.

[edit] Example use

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