- friends to function
In some cases it is convenient to allow certain functions to access the private members of a class withoutallowing access to the entire program. The friend mechanism allows a class to grant functions access to its nonpublic members.
such as
class Screen {
friend istream& operator>>( istream&, Screen& );
...
}
ostream& operator>> ( ostream& os, const Screen& s )
{
os << s._height << s._screen;
return os;
}
COMMENTS: operator>> is the most popular usage of friends keyword, because it must be declared as friend function.
- friends to whole class or namespace.
本文介绍了C++中友元机制的基本概念及其应用场景。通过友元机制,特定函数可以访问类的私有成员,而无需将整个程序暴露给这些函数。文章通过具体示例展示了如何声明和使用友元函数。
3796

被折叠的 条评论
为什么被折叠?



