我有一个 Controller
类,它有 Parent
类作为 friend
:
class Controller {
// I can use x;
};
class Parent {
friend class Controller;
int x;
};
有没有一种方法可以指定Parent
的whole 子类层次结构将Controller
作为友元,而无需在每个类中明确指定它我有 300 个?
最佳答案
回复
” Is there a way to specify that the whole subclass hierarchy of
Parent
will haveController
as friend without explicitly specifying it in every single class of the 300 I have?
不可以,但是您可以让Parent
为其派生类提供相关功能。
请注意,这会回答您的原始问题和修改后的(根本改变的)问题。
写信。原来的问题
评论
// I can use x;
在 Controller
类中,其中 x
是 Parent
的成员,表示您认为在 Controller 中指定的友元
类授予 Controller
访问 Parent
私有(private)部分的权限。
C++友元是单向的,反之亦然。
关于c++ - 子类的类友元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702468/