Hi All,
i am curious to know how to set the value (value=10) through B's constructor in below programme. below is example of multiple inheritance. As construction always happens from right to left so it always call the 'C' class's constructor and set the value 20 but
as per my requirement if i want to call only B class's constructor and set the value 10 it is not happening. so how does this virtual inheritance solve this problem here.
#include<iostream>
using namespace std;
class A
{
int x;
public:
void setX(int i) {x = i;}
void print() { cout << x; }
};
class B: virtual public A
{
public:
B() { setX(10); }
};
class C: virtual public A
{
public:
C() { setX(20); }
};
class D: public B, public C {
};
int main()
{
D d;
d.print();
return 0;
}