Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is because you're not destroying <code>A(1)</code> you're assigning <code>A(2)</code> to it, let's extend your example by adding <a href="http://en.wikipedia.org/wiki/Assignment_operator_%28C++%29" rel="nofollow">assign operator</a>:</p> <pre class="lang-cpp prettyprint-override"><code>class A { int a; public: A(int a) : a(a) { cout &lt;&lt; "Constructor a=" &lt;&lt; a &lt;&lt; endl; } ~A() { cout &lt;&lt; "Destructor a=" &lt;&lt; a &lt;&lt; endl; } void print() { cout &lt;&lt; "Print a=" &lt;&lt; a &lt;&lt; endl; } A &amp;operator=(const A &amp;other) { cout &lt;&lt; "Assign operator old=" &lt;&lt; a &lt;&lt; " a=" &lt;&lt; other.a &lt;&lt; endl; a = other.a; } }; </code></pre> <p>Which will result to:</p> <pre class="lang-none prettyprint-override"><code>[vyktor@grepfruit tmp]$ ./a.out Constructor a=1 Print a=1 Constructor a=2 Assign operator old=1 a=2 &lt;- This line explains why destructor is not called Destructor a=2 Print a=2 Destructor a=2 </code></pre> <p>If you have one of these implemented:</p> <ul> <li><strong>Destructor</strong> - Destruct all the object's members</li> <li><strong>Copy constructor</strong> - Construct all the object's members from the equivalent members in the copy constructor's parameter</li> <li><strong>Copy assignment operator</strong> - Assign all the object's members from the equivalent members in the assignment operator's parameter</li> </ul> <p>you should implement all of them. This is called <a href="http://en.wikipedia.org/wiki/Rule_of_three_%28C++_programming%29" rel="nofollow">rule of three</a>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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