I have the the following classes:
class Base
{
public:
Base() { x = 3; }
int x;
virtual void foo() {};
};
class Med1 : public virtual Base
{
public:
int x;
Med1() { x = 4; }
virtual void foo() {};
};
class Med2 : public virtual Base
{
public:
virtual void goo() {};
virtual void foo() {};
};
class Der : public Med1, public Med2
{
public:
Der() {}
virtual void foo() {};
virtual void goo() {};
};
And the following code:
Base* d = new Der;
d->foo();
cout << d->x;
Output:
3
Why is that? Med1 constructor is called after Base constructor. I’m guessing it’s setting Med1::x, and not Base::x, but why is Der::x the same as Base::x and not Med1::x. Why is there no ambiguity?
dis a pointer toBase, sod->xrefers unambiguously toBase::x. There would only be an ambiguity if it were a pointer toDer.