I have 2 base classes (B1
and B2
) which are derived from common Base class(B), where they have a common variable (let: int x;
from base B), in 1st base x=0
, in the 2nd base x=10
(default values given in B1,B2 constructors).
Now if I derive one more class (class D : virtual public B1, virtual public B2{}
).
Here only one copy of x
will be available as per virtual concept, now if I try to access x
value with derived class object which instance of x
I will get in O/p (x=0
or x=10
), and why?
Thanks in advance.
This is not the right place ( if you are not further deriving ) to use virtual base class. Perhaps you are mixing concepts.
In this case it's meaningful to make base classes virtual as there are two paths to class A when you see through D.
Your case is :-