Note that there are some explanatory texts on larger screens.

plurals
  1. POdiamond shaped multiple inheritance pattern
    primarykey
    data
    text
    <p>Below is a diamond problem faced in multiple inheritance, </p> <pre><code>class Base { public: Base() { cout &lt;&lt; "Empty Base constructor " &lt;&lt; endl; } Base(const string &amp; strVar) { m_strVar = strVar; cout &lt;&lt; m_strVar &lt;&lt; endl; } virtual ~Base() { cout &lt;&lt; "Empty Base destructor " &lt;&lt; endl; } virtual const string &amp; strVar() const { return m_strVar; } string m_strVar; }; class Derived1: public virtual Base { public: Derived1() { cout &lt;&lt; "Empty Derived1 constructor " &lt;&lt; endl; } Derived1(const string &amp; strVar) : Base(strVar) { cout &lt;&lt; " Derived1 one arg constructor" &lt;&lt; endl; } ~Derived1() { cout &lt;&lt; "Empty Derived1 destructor " &lt;&lt; endl; } }; class Derived2: public virtual Base { public: Derived2() { cout &lt;&lt; "Empty Derived2 constructor " &lt;&lt; endl; } Derived2(const string &amp; strVar) : Base(strVar) { cout &lt;&lt; "Derived2 one arg constructor" &lt;&lt; endl; } ~Derived2() { cout &lt;&lt; "Empty Derived2 destructor " &lt;&lt; endl; } }; class Derived: public Derived1, public Derived2 { public: Derived(const string &amp; strVar) : Derived1(strVar), Derived2(strVar) { cout &lt;&lt; "Derived Constructor " &lt;&lt; endl; } ~Derived() { cout &lt;&lt; "Empty Derived destructor " &lt;&lt; endl; } }; int main() { Derived derObj ("Print this if you can ! "); } </code></pre> <p>the output i get is </p> <ol> <li>Empty Base constructor</li> <li>Derived2 one arg constructor</li> <li>Derived1 one arg constructor</li> <li>Derived Constructor</li> <li>Empty Derived destructor</li> <li>Empty Derived2 destructor</li> <li>Empty Derived1 destructor</li> <li>Empty Base destructor</li> </ol> <p>i wonder why my derObj parameter i.e "Print this if you can" is not printed and output is not like</p> <ol> <li>Empty Base constructor</li> <li>Derived2 one arg constructor</li> <li>Print this if you can!</li> <li>Derived1 one arg constructor</li> <li>Derived Constructor</li> <li>Empty Derived destructor</li> <li>Empty Derived2 destructor</li> <li>Empty Derived1 destructor</li> <li>Empty Base destructor</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload