c++ - Multiple inheritance -



c++ - Multiple inheritance -

i have 2 base of operations classes (b1 , b2) derived mutual base of operations class(b), have mutual variable (let: int x; base of operations b), in 1st base of operations x=0, in 2nd base of operations x=10 (default values given in b1,b2 constructors).

visually:

class b { int x; protected: b(int x) : x{x}{} }; class b1 : public b { protected: b1() : b(0){} }; class b2 : public b { protected: b2() : b(10){} };

now if derive 1 more class:

class d : virtual public b1, virtual public b2 { public: d() : b1{}, b2{}{} };

here 1 re-create of x available per virtual concept, if seek access x value derived class object instance of x in o/p (x=0 or x=10), , why?

in order utilize virtual inheritance, base of operations b must declared virtual in both b1 , b2. without that, have non-virtual inheritance of b.

if have non-virtual inheritance, have 2 b bases in d, can't access x in d without qualifying b1::x or b2::x

if have virtual inheritance, have 1 b , 1 x, 2 assignments (x=0 , x=10) happen in whichever order did them in, , whichever 1 later overwrite value set before 1 (much simple variable x 2 assignments).

c++ multiple-inheritance

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

SQL Server : need assitance parsing delimted data and returning a long concatenated string -