0
class X {
public:
int i;
};

class Y : virtual public X {};

class Z : public Y, virtual public X {
public:
void f() {
    cout << i;
}
};

Why in this type of inheritance is object X inside Z found in only 1 instance and there is no ambiguity but if i did public X there's ambiguity. Isnt declaring class as virtual only to say when something is derived from Z later on , Z shall share X among other classes that inherited X as virtual?

  • The fact that Z inherits from Y which virtually inherits from X does nothing to prevent you from writing a version of Z that _also_ inherits, nonvirtually, from X. Right now there's no reason for you to bother specifying that Z inherits directly from X at all. – Nathan Pierson Nov 30 '21 at 15:38
  • So actualy Z didnt inherit another X when i typed virtual public X, i just specified that later on it shall look for other other sub-object to share X with? Im learning this from my university book so i got all consufed about this type of scenario. – askingRealQuestions Nov 30 '21 at 15:41

0 Answers0