2

Is there an issue with such an inheritance tree:

Parent ---- Subject
|         /
|        /
Child --/

Or in code:

class Subject
{
public:
    void AddObserver( Observer *aObserver );
protected:
    ObserversList mObserversList;
};

class Parent: public Object,
              public virtual Subject
{
};

class Child: public Parent,
             public virtual Subject
{
};
0

1 Answer 1

1

Compilation-wise there is no issue with this kind of inheritance. As both the bases coming to Child are virtual inherited.

The only thing one must know is that, while constructing Parent(), the order of construction will be Subject() followed by Object(). Same thing is applicable for Child(). The order will be Subject(), Object(), Parent().

Sign up to request clarification or add additional context in comments.

2 Comments

OK, so if I understand correctly, Child will only have a 'single' Subject interface. Also, would the answer remain the same if I don't use virtual with public Subject?
@Izhaki, Yes, Child will have only one Subject base inside it, which is first to be constructed. No, things won't be same if you remove virtual from either Parent or Child. Some compilers may support having just one virtual inheritance, but that's not allowed by standard. All the Subject coming towards Child must be virtual inherited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.