Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在学习friendC++ 中的关键字,我想知道为什么有一个非成员函数并friend在你可以将非成员函数设为成员函数时使用关键字?我希望我的问题足够清楚,谢谢!
friend
因为有时您需要创建一个重载运算符,其中您的类类型位于右侧。这必须作为一个自由函数来实现。经典例子:
ostream& operator<<(ostream& str, my_type const& my) { // print out `my` into `str`---requires `friend` if using // private members of `my_type` return str; }