c++ - 子类的类友元

标签 c++ friend

我有一个 Controller 类,它有 Parent 类作为 friend:

class Controller {

   // I can use x;

};

class Parent {
   friend class Controller;
   int x;
};

有没有一种方法可以指定Parentwhole 子类层次结构将Controller 作为友元,而无需在每个类中明确指定它我有 300 个?

最佳答案

回复

Is there a way to specify that the whole subclass hierarchy of Parent will have Controller as friend without explicitly specifying it in every single class of the 300 I have?

不可以,但是您可以让Parent 为其派生类提供相关功能。

请注意,这会回答您的原始问题和修改后的(根本改变的)问题。


写信。原来的问题

enter image description here

评论

// I can use x;

Controller 类中,其中 xParent 的成员,表示您认为在 Controller 中指定的友元 类授予 Controller 访问 Parent 私有(private)部分的权限。

C++友元是单向的,反之亦然。

关于c++ - 子类的类友元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702468/

相关文章:

c++ - 如何保留多维Vector?

c++ - 类中定义的友元函数模板是否可供查找? clang++ 和 g++ 不同意

c++ - 模板化 << friend 在与其他模板化 union 类型相互关联时不工作

.net - friend 引用课?

c++ - 在 C++ 标准中,哪里声明了 const 右值引用不绑定(bind)到左值?

c++ - 为什么reinterpret_cast不是constexpr?

iOS 从我的好友列表中对使用某个特定应用程序的 facebook 好友进行排序

C++如何避免使用自制的侵入式列表的友元模板函数

c++ - 如何在 Linux 上使用 Gecode Framework 运行 .cpp 文件?

c++ - 限制嵌套结构中的构造函数范围