C++ Member Variable value changes based on whether or not it is printed out
<p>I have a class <code>ZoneDeVie</code> containing a vector of vectors of <code>Bacterie*</code>. The <code>Bacterie</code> class contains an int value <code>energie</code> (set to 10 by default) and a <code>toString()</code> function which prints the value. In the <code>ZoneDeVie</code> constructor, I build the 2D table, populating each cell with a default instance of a <code>Bacterie</code>. Then, in my main method, I'm testing by printing the <code>toString()</code> of the last <code>Bacterie</code> in the table. For some reason, it returns a random, obnoxiously-large int (usually something like: 3753512); however, if I make a call to the Bacterie's <code>toString()</code> method in the constructor of ZoneDeVie, the main method will print out correctly.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;
using namespace std;

class Bacterie {
public:
    Bacterie() { this-&gt;energie = 10; }
    string toString() {
        stringstream ss;
        ss &lt;&lt; "Energie: " &lt;&lt; this-&gt;energie;
        return ss.str();
    }
protected:
    int energie;
};

class ZoneDeVie {
public:
    ZoneDeVie(int width, int height) {
        Bacterie* bac = new Bacterie();

        // without this [following] line, the call to `toString`
        // in the main method will return an obnoxiously-large value
        //bac-&gt;toString();
        for (int i=0; i&lt;height; i++) {
            vector&lt;Bacterie*&gt; bacvec = vector&lt;Bacterie*&gt;();
            this-&gt;tableau.push_back(bacvec);
            for (int j=0; j&lt;width; j++) {
                this-&gt;tableau[i].push_back(bac);
            }
        }
    }
    vector&lt;vector&lt;Bacterie*&gt; &gt; tableau;
};

int main(int argc, char *argv[]) {
    int x,y;
    x = 9; y = 39;
    ZoneDeVie zdv = ZoneDeVie(10,40);
    cout &lt;&lt; "zdv(" &lt;&lt; x &lt;&lt; "," &lt;&lt; y &lt;&lt; ") = " &lt;&lt; zdv.tableau[x][y]-&gt;toString();

    return 0;
}
</code></pre>

<p><strong>output (with a call to "toString()" in ZoneDeVie's constructor):</strong> <code>zdv(9,39) = Energie: 10</code></p>

<p><strong>output (w/o a call to "toString()" in ZoneDeVie's constructor):</strong> <code>zdv(9,39) = Energie: 4990504</code></p>

<p>Why in the world do I need to call my toString() method before calling it in the main method in order for it to behave as expected?</p>
