朋友,从班级继承的所有课程
Friend all classes that inherit from a class
这更像是一种智力上的好奇心,而不是实际问题。我想知道在C 中是否有办法进行以下操作:让A
成为一类。我想与所有从A
继承的类结合B
朋友。
在您说之前:我显然知道友谊不是继承的。我想做的是使用和每个类C
对B
进行template friend
语句,也许使用Sfinae,以便C
从A
继承。
这是可能的吗?我试图从最简单的情况开始:您可以与所有其他班级结交一堂朋友吗?显然,我知道这没有任何意义,一个人可以将事物公开,但是也许从这个起点可以完善事情,只能选择从A
继承的类。
一个工作要解决的是使用继承的"键"访问。
// Class to give access to some A members
class KeyA
{
private:
friend class B; // Give access to base class to create the key
KeyA() = default;
};
class A
{
public: // public, but requires a key to be able to call the method
static void Foo(KeyA /*, Args... */) {}
static void Bar(KeyA /*, Args... */) {}
};
class B
{
protected:
static KeyA GetKey() { return KeyA{}; } // Provide the key to its whole inheritance
};
class D : public B
{
public:
void Foo() { A::Foo(GetKey()); } // Use A member with the key.
};
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 头文件-继承c++
- 为什么在保护模式下继承升级不起作用
- 通过继承类使用来自不同命名空间的运算符
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 混合组合和继承的C++问题
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 从类继承时,继承的类是否会通过父类重新定义继承的变量
- 公共与私人继承
- 如何创建从同一类继承的不同对象的向量
- 为什么C++需要公共继承,忽略朋友声明,才能使动态向下工作?
- 有没有办法C++将给定类的功能限制为仅另一个类(不使用继承,朋友)?
- 朋友,从班级继承的所有课程
- 朋友功能是否继承?为什么基类朋友功能在派生的类对象上工作
- 沿类层次结构的私人继承,为什么在整个层次结构中都需要朋友
- 朋友功能和继承在CPP中
- 继承和朋友在C++中起作用
- 为什么 GCC 不允许在你是朋友时从私有嵌套类继承
- 奇怪的重复模板模式 - 继承和朋友