Data of destroyed C++ object still accessible?
<p>In the following code, the <code>getObj()</code> function returns a reference to a local object. This is obviously very bad since the object gets destroyed when the function returns (the ctor and dtor outputs emphasize the objects lifetime). As expected, the compiler (gcc44) gives a respective warning.</p>

<pre><code>#include &lt;iostream&gt;

class Blah {

private:
    int a_;

public:
    Blah(int a) : a_(a) { std::cout &lt;&lt; "Constructing...\n"; }
    ~Blah() { std::cout &lt;&lt; "...Destructing\n"; }
    void print() { std::cout &lt;&lt; a_ &lt;&lt; "\n"; }
};

Blah&amp; getObj() 
{
    Blah blah(3);
    return blah; // returning reference to local object
}

int main()
{
    Blah&amp; b = getObj();
    b.print(); // why does this still output the correct value???

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

<p>However, calling <code>print()</code> on the apparently destroyed object still prints the correct value of the private variable <code>a_</code>. This is the output:</p>

<p>Constructing...<br>
...Destructing<br>
3  </p>

<p>How can this be? I would have expected all of the objects data to be destroyed as well.</p>
