What happens in C++ when I pass an object by reference and it goes out of scope?
<p>I think this question is best asked with a small code snippet I just wrote:</p>

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

using namespace std;

class BasicClass
{
public:
    BasicClass()
    {
    }
    void print()
    {
        cout &lt;&lt; "I'm printing" &lt;&lt; endl;
    }
};

class FriendlyClass
{
public:
    FriendlyClass(BasicClass&amp; myFriend) :
        _myFriend(myFriend)
    {
    }
    void printFriend()
    {
        cout &lt;&lt; "Printing my friend: ";
        _myFriend.print();
    }
private:
    BasicClass&amp; _myFriend;
};

int main(int argv, char** argc)
{
    FriendlyClass* fc;
    {
        BasicClass bc;
        fc = new FriendlyClass(bc);
        fc-&gt;printFriend();
    }
    fc-&gt;printFriend();
    delete fc;
    return 0;
}
</code></pre>

<p>The code compiles and runs fine using g++:</p>

<pre><code>$ g++ test.cc -o test
$ ./test
Printing my friend: I'm printing
Printing my friend: I'm printing
</code></pre>

<p>However, this is not the behavior I was expecting. I was expecting some sort of failure on the second call to <code>fc-&gt;printFriend()</code>. Is my understanding of how the passing/storing by reference works incorrect or is this something that just happens to work on a small scale and would likely blow up in a more sophisticated application?</p>
