简体   繁体   English

C ++-多重继承

[英]C++ - Multiple Inheritance

I have a requirement which will form the diamond dread pattern. 我有一个要求,它将形成钻石恐怖图案。 But I have come across comments from stack overflow mates that if the virtual base class is a pure abstract class, then the diamond pattern is not so much of a concern. 但是我遇到了堆栈溢出伙伴的评论,即如果虚拟基类是纯抽象类,那么菱形图案就没什么问题了。 Can someone please expand on this as to why it is so? 有人可以解释为什么会这样吗?

Multiple inheritance in C++ is a powerful, but tricky tool, that often leads to problems if not used carefully. C ++中的多重继承是一个功能强大但棘手的工具,如果使用不仔细,通常会导致问题。

Following article will teach you how to use virtual inheritance to solve some of these common problems programmers run into. 下一篇文章将教您如何使用虚拟继承来解决程序员遇到的一些常见问题。

Solving the Diamond Problem with Virtual Inheritance 用虚拟继承解决钻石问题

try to implement dimond as follow:Article gives very good explanation 尝试实现dimond如下:文章给出了很好的解释

class storable 
{
        public:
        storable(const char*);
        virtual void read()=0; //this becomes pure virtual making storable an abstract
        virtual void write(); //class
        virtual ~storable();
        private:
        ....
}

class transmitter: public virtual storable 
{
        public:
        void write()
        {
                read();
                ....
        }
} 

class receiver: public virtual storable
{
        public:
        void read();
}

class radio: public transmitter, public receiver
{
        public:
        ...
}

int main()
{
        radio *rad = new radio();
        receiver *r1 = rad;
        transmitter *r2 =rad;

        rad->write();
        r1->write();
        r2->write();
        return 1;
}

I am not sure of how much you know or don't know about virtual inheritance or the diamond pattern. 我不确定您对虚拟继承或菱形样式了解多少。 The simple answer is that if inheritance is not virtual (from the root of the diamond) then instead of a diamond you have a V , where the complete type contains two independent copies of the base class. 一个简单的答案是,如果继承不是虚拟的(从菱形的根部开始),那么您将拥有一个V ,而不是菱形,其中完整类型包含基类的两个独立副本。 Virtual inheritance is basically telling the compiler that you only want one copy of the base in the complete type (most derived), regardless of how many intermediate types mention that base as virtual in their inheritance relationship. 虚拟继承基本上是告诉编译器,您只需要一个完整类型的基本副本(大多数派生),而不管有多少中间类型在其继承关系中将该基类称为虚拟。

Of course, the approach brings up its own problems as for example the fact that encapsulation is somehow broken (the derived type must know of the inheritance relationship of is bases with respect to their virtual base in order to call the constructors), and the operations on the virtual base subobject will be somehow more complex and expensive. 当然,该方法会带来自身的问题,例如封装被某种程度破坏的事实(派生类型必须知道基相对于其虚拟基的继承关系才能调用构造函数),以及操作虚拟基础子对象上的对象将以某种方式更加复杂和昂贵。

Have a look at this link - How can I avoid the Diamond of Death when using multiple inheritance? 看一下此链接- 使用多重继承时如何避免死亡钻石?

I believe the comments will give you some insight into this 我相信这些评论将使您对此有所了解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM