c++ - friend 声明的放置

标签 c++ class friend protected private-members

friend 子句在类中的位置是否重要(即在 protected block 内而不是 private block 内)?

最佳答案

不,它没有。

class X
{
public:
    friend class A;
private:
    friend class B;
protected:
    friend class C;
};

所有三个类现在都是 X 的 friend 并且共享完全相同的特权。

一个好的约定是将所有友元声明组合在一起以提高可见性,但这只是风格。

11.4 好友

9) A name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration. The meaning of the friend declaration is the same whether the friend declaration appears in the private, protected or public (9.2) portion of the class member-specification.

关于c++ - friend 声明的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620908/

相关文章:

ios - 类变量在重新初始化后保持不变

c++ - C++中的模板类重载运算符ostream不起作用

c++ - 当类和函数具有单独的模板参数时,在类定义之外定义友元函数

c++ - 删除元素后,(c++) vector 会缩小吗?

c++ - 将函数指针传递给一个函数,该函数通过引用获取另一个函数

c# - 错误 : member names cannot be the same as their enclosing type

类中的php回调函数

c++ - 为什么不能在类中引用转发声明的 friend 类?

c++ - 按字母顺序对 JSON 值进行排序 C++

c++ - 如何在 Visual Studio (C++) 中找到应用程序的入口点