0

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?

notifyroy
  • 139
  • 1
  • 8

3 Answers3

5

Multiple inheritance in C++ is a powerful, but tricky tool, that often leads to problems if not used carefully.

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

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;
}
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
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. 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.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Typically the virtual bases will be abstract classes, without data members, and thus with only a (machine generated) default constructor. There may be rare exceptions, but otherwise, there's no need for the author of the derived class to be aware of the virtual inheritance. – James Kanze Jan 31 '12 at 13:27
  • @JamesKanze: To be honest, I have only seen the virtual bases used in a few times, and while some did comply with what you say (pure abstract class) in some others it wasn't. Again, my experience with virtual base classes is very limited. – David Rodríguez - dribeas Jan 31 '12 at 16:57
  • I think it depends a lot on the application. I've seen some applications which use them extensively; others much less, or not at all. (I've also seen applications which abuse them extensively.) In general, where they were well used, virtual bases with data were rare (and mostly in machine generated code). – James Kanze Jan 31 '12 at 17:26
  • I guess the answer is even if the virtual base itself is a pure abstract class with no member data, you still probably don't want to remove the virtual inheritance within classes derived from the virtual base. You can use fully qualified names to resolve any ambiguities that arise. Please refer to the link [link](http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.11} – notifyroy Feb 01 '12 at 01:13
0

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

Community
  • 1
  • 1
Kris Dunning
  • 127
  • 2
  • 9