部分隐藏的继承树
Partially hidden inheritance tree
我有一个这样的类树:
class A;
class B : public A;
然后我想创建一个从类 B 派生的类。但是我希望该派生对外部成员以及从C类继承的任何其他人隐藏
class C : private B;
void test() {
C c;
B *b = &c; // compiler error: B in C is private and is therefore not type compatible. This is desired.
}
但是,我也想揭示类 A 的继承.在这种情况下隐藏类 B 也会隐藏类 A。
void test2() {
C c;
A *a = &c; // Compiler error: A is only accessible through B which is not possible with a private inheritance of B. This is not desired; this conversion should be possible.
}
我可以再次从 A 继承,但如果 A 有的话,这显然会创建重复的成员变量。我可以创建类 A 的虚拟继承,但是我认为它不会产生我想要的确切效果,因为这会影响整个树而不是这一段(对吧?
我想显而易见的解决方案是创建一个类型转换成员函数:
class C : private B {
A * turn_into_A() {
// Since B is an A and we are still in the scope of C, this will succeed
return this;
}
};
但是,我宁愿避免显式类型转换,例如这种情况,
任何理智的人都可能会告诉我我做错了。他们可能是对的。但是我只是为了知识而想知道:有没有办法在没有虚拟继承或显式成员函数的类型转换的情况下做到这一点?
我找到了一个可行的解决方案:
class A {
public:
void somethingA() {
std::cout << "a" << std::endl;
return;
}
};
class B :
public A {
public:
void somethingB() {
std::cout << "b" << std::endl;
return;
}
};
class C :
private B {
public:
using B::A; // While B is private (and hidden), this exposes access to B::A
void somethingC() {
std::cout << "c" << std::endl;
return;
}
};
int main(int argc, char **argv) {
C c;
B* b = &c; // Compiler error: cannot convert because B is private (desired)
A* a = &c; // Okay! (required)
c.somethingC();
c.somethingB(); // Compiler error: private. (desired)
c.somethingA(); // Compiler error: A is exposed, but not A's members. This can be solved by adding 'using B::A::somethingA()' in class declaration (undesired but acceptable in my situation)
a->somethingA(); // Okay! (of course)
}
它并不完美,因为它只公开 C 以便能够转换为 A(就我而言,无论如何我最终都会这样做,所以这很好)。但是,它不会直接公开 A 的成员以允许 C 用作 A,例如,除非您特别公开 B::A::somethingA,否则您不能调用 c::somethingA()。
继承描述了
IS-A 关系。因此,在对象模型中,B IS-A A,C IS-A B。那么,你为什么不使用
class C : public B { ...};
这样您就可以根据需要将 C 对象视为 B 对象以及 A 对象。希望有帮助。
相关文章:
- 继承函数的重载解析
- 继承期间显示未知行为的子类
- 名称隐藏对静态函数继承的实例使用
- C++继承模板类:隐藏成员变量
- QScopedPointer 是否隐藏了多重继承
- 在继承的类构造函数中使用隐藏成员
- 使用私有继承来隐藏实现是个好主意吗
- C++隐藏继承的类
- 由多重继承隐藏的嵌套类
- 对继承的类隐藏的名称
- 具有不同签名的两个函数的继承隐藏了非虚函数
- 我如何使用接口隐藏我的实现,并且仍然使用继承构建类
- 为什么继承的函数隐藏在这个代码的基类中
- C++通过继承隐藏变量
- c++继承和名称隐藏
- 在c++中使用私有继承时是否可能隐藏重载方法?
- 访问隐藏的继承成员
- c++继承名称隐藏
- 部分隐藏的继承树
- 我如何使用 no除非我从继承列表中选择特定的隐藏函数