Destructor query
<p>I have this below program, where I am passing a vector by reference to a function <code>myFunc</code> and inside this function, I am adding few elements to the vector.</p>

<p>I am not freeing object creating with new, for now ignore the memory leak due to this.</p>

<p>After <code>myFunc()</code> execution is complete I am printing the variables ctor and dtor
to know how many times constructor and destructor were called.</p>

<p>Output is:</p>

<pre><code>Before Exiting 5 7
</code></pre>

<p>I am creating 5 objects so <code>ctor</code> is <code>5</code>. But why is <code>dtor</code> <code>7</code>? From where do the extra two counts? Am I missing something?</p>

<pre><code>#include
#include
using namespace std;

static int ctor = 0;
static int dtor = 0;

class MyClass
{
public:
    MyClass(int n)
    {
        i = n;
        ctor++;
        // cout &lt;&lt; "Myclass ctor " &lt;&lt; ctor &lt;&lt; endl; 
    } 

    ~MyClass()
    {
        dtor++; 
        // cout &lt;&lt; "Myclass dtor" &lt;&lt; dtor &lt;&lt; endl;
    }

private: 
    int i;
}; 

void myFunc(vector&lt;MyClass&gt; &amp;m);

void myFunc(vector&lt;MyClass&gt; &amp;m)
{
    MyClass *mc;

    for(int i = 0; i &lt; 5; i++)
    {
        mc = new MyClass(i);
        m.push_back(*mc);
    }
}

int main()
{

    vector&lt;MyClass&gt; m;
    vector&lt;MyClass&gt;::iterator it;

    myFunc(m);

    cout &lt;&lt; "Before Exiting " &lt;&lt; ctor &lt;&lt; " " &lt;&lt; dtor &lt;&lt; endl;
}
</code></pre>
