friend specifier
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
本章尚未完成 原因:needs cleanup |
[编辑] 说明
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.
[编辑] 例如使用
friend ostream& operator<<(ostream& out, const T& o); friend istream& operator>>(istream& in, T& o); friend swap(T& left, T& right);