friend specifier

来自cppreference.com
< cpp‎ | language

 
 
C++语言

|- class="t-nv-h1" | colspan="5" | 一般主题 |- | |- |

预处理器
注释

|

|-

|- class="t-nv-h1" | colspan="5" | 流程控制 |-

条件执行语句

| |- |

|

|-

循环语句

| |- |

|

|-

跳转语句

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 函数 |-

函数声明 Lambda函数声明 inline说明符 异常说明符 (已弃用) noexcept说明符 (C++11)

|- class="t-nv-h1" | colspan="5" | Exceptions |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 命名空间 |- | |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类型 |- | |- |

|

|-

说明符

| |- |

decltype (C++11)
auto (C++11)
alignas (C++11)

|

|-

存储持续期说明符 Initialization

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 表达式 |- | |- |

值类别
求值顺序

|

|-

替代表示 字面量 布尔型 - 整型 - 浮点型 字符型 - 字符串 - nullptr (C++11) 用户定义 (C++11)

|- class="t-nv-h1" | colspan="5" | Utilities |-

属性 (C++11) Types typedef声明 类型别名声明 (C++11) Casts 隐式类型转换 - 显示类型转换 static_cast - dynamic_cast const_cast - reinterpret_cast 内存分配

| |- |

|

|-

|- class="t-nv-h1" | colspan="5" | 类 |- | |- |

|

访问控制
friend说明符

|-

类特有的函数属性

| |- |

|

explicit (C++11)
static

|-

静态成员函数

| |- |

|

|- |- class="t-nv-h1" | colspan="5" | 模板 |- | |- |

|

|- |- class="t-nv-h1" | colspan="5" | 杂项 |- | |- |

|

|-

 

[编辑] 说明

Friend关键字用于授予访问类成员以外的类。例如,假设你的类有私有或受保护的成员。您可以访问这些成员只能从同一类或派生类。通过使用关键字的朋友,您授予访问您的类的友元函数或友元类的private和protected成员。的朋友的一些比较常见的用途是经营者“和运算符<<和交换功能.
原文:
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.
这段文字是通过 Google Translate 自动翻译生成的。
您可以帮助我们检查、纠正翻译中的错误。详情请点击这里

[编辑] 例如使用

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


来自“http://zh.cppreference.com/mwiki/index.php?title=cpp/language/friend&oldid=28853