C++解决没有虚拟继承的钻石继承问题
C++ Solving Diamond Inheritance Without Virtual Inheritance
我有以下无法编译的钻石类结构:
class Base{
int a;
public:
virtual void doSomething();
};
class NotMineToTouch : public Base {};
class MyParentClass : public Base {};
class EvilDiamond : public NotMineToTouch, public MyParentClass {};
// I need these methods (that I cannot necessarily edit) to work for an EvilDiamond
void processBase (Base* b) { b->doSomething; /*...*/} // Cannot edit
void processParent (MyParentClass* p) { p->doSomething; /*...*/} // Can edit
void processNotMine (NotMineToTouch* n) { n->doSomething; /*...*/} // Cannot edit
我知道正常的解决方案是从Base
继承;但是,我不允许更改NotMineToTouch
(或Base
(。 有没有其他解决方案? 我被允许随心所欲地改变MyParentClass
和EvilDiamond
;但是,EvilDiamond
必须继承MyParentClass
和NotMineToTouch
,MyParentClass
必须继承Base
,不能继承EvilDiamond
。
我挑战以下断言:
EvilDiamond 必须继承自 MyParentClass 和 NotMineToTouch
您可以按照以下思路执行一些操作(取决于您的体系结构(:
class EvilDiamond;
class NotMineToTouchImpl : public NotMineToTouch {
EvilDiamond* tgt_;
public:
NotMineToTouchImpl(EvilDiamond* tgt) : tgt_(tgt) {}
... implement NotMineToTouch here, using tgt_ where you would have used this
};
class MyParentClassImpl : public MyParentClass {
EvilDiamond* tgt_;
public:
MyParentClassImpl(EvilDiamond* tgt) : tgt_(tgt) {}
... implement Base here, using tgt_ where you would have used this
};
class EvilDiamond {
friend class NotMineToTouchImpl;
friend class MyParentClassImpl;
// Creating permanent instances of the API classes
// may or may not be appropriate in your case.
NotMineToTouchImpl nmti_;
MyParentClassImpl pci_;
public:
EvilDiamond () : nmti_(this), pci_(this) {}
NotMineToTouchImpl* getAsNotMineToTOuch() {return &nmti_;}
MyParentClassImpl * getAsParentClass() {return &pci_;}
};
你没有钻石,因为你不使用虚拟继承。
您目前有一些"Y">继承(EvilDiamond
有 2Base
(。
在不更改类的情况下,您可以添加重载来指示编译器要做什么:
void processBase (EvilDiamond* evil) {
processBase(static_cast<NotMineToTouch*>(evil)); // Use NotMineToTouch::Base
processBase(static_cast<MyParentClass*>(evil)); // Use MyParentClass::Base
}
相关文章:
- 模板继承问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- SIGSEGV 因为基/派生类的继承问题
- C++解决没有虚拟继承的钻石继承问题
- Arduino C 中的继承问题
- C++ 继承问题:未定义对"vtable"的引用
- 班级设计和继承问题
- C 继承问题错误C2084功能已经具有主体
- 如何在C++中解决网格继承问题
- 多重继承问题 C++
- "{"令牌 c++ 继承问题之前的另一个预期类名
- C 模板继承问题(警告和错误)
- 协助解决C++的继承问题
- 在我的示例中,实际上有一些C 继承问题
- 继承问题
- C++继承问题
- C++初学者继承问题
- 公共/受保护/私有继承问题
- 函数继承问题
- 引用类型和继承问题