C++ static counter not increasing correctly
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/12567416/why-does-my-destructor-appear-to-be-called-more-often-than-the-constructor">Why does my destructor appear to be called more often than the constructor?</a>  </p>
</blockquote>



<p>I have a <code>static sCounter</code> and <code>int id</code> to order my instances with id's.</p>

<p>When grouping them in a vector, this works fine.. ID's are setted corrrectly:</p>

<pre><code>Task t("1st Task");
Task t2("2nd Task");
    Task t3("3rd Task");

std::vector&lt;Task&gt; myTasks;
myTasks.push_back(t);
myTasks.push_back(t2);
myTasks.push_back(t3);
</code></pre>

<p>task number one has ID 0, the second one: ID 1 and the third one ID 2.
However, if I do this:</p>

<pre><code>std::vector&lt;Task&gt; myTasks;

myTasks.push_back(
   Task("First Task"));
myTasks.push_back(
   Task("Second Task"));
myTasks.push_back(
   Task("Third Task"));
</code></pre>

<p>The static sCount values starts giving strange results:</p>

<p>the first one gets ID 1, the second also ID 1, and the third gets ID 0.</p>

<p>Why does creating object like this mess with the static counter?</p>

<p>The sCounter gets increased on the constructor, and decreased on the destructor:</p>

<pre><code>int Task::sCount = 0;

Task::Task(std::string text) {
    this-&gt;setText(text);
    this-&gt;setStatus(1);
    time_t now = time(0);
    timestamp = *localtime(&amp;now);
    std::cout &lt;&lt; "Count from " &lt;&lt; sCount;
    sCount++;
    std::cout &lt;&lt; " to --&gt; " &lt;&lt; sCount &lt;&lt; "\n";
    this-&gt;setID(); // set the current sCount to ID
}
Task::~Task() {
    --sCount;
}
</code></pre>

<p>sCounter is a private static int: <code>static int sCount;</code></p>
