#include <iostream>
using namespace std;
class Foo
{
int _bar;
public:
Foo() : _bar(0) { }
int Bar() { return _bar; }
void SetBar(int value) { _bar = value; }
}; //end class
Foo& BrokenFoo()
{
return Foo();
} //end f()
int main()
{
Foo& f = BrokenFoo();
f.SetBar(10);
cout << f.Bar() << endl;
system("PAUSE");
} //end f()
Imagine my surprise when, not only did the application not throw an exception but the output was 10.
What gives? We were using Visual Studio 2008. When we built the application in Debug Mode the output was -858993460, and in both cases we got a compiler warning, and I know I've hunted down this very bug more than once in the past. But, it the application seemed to allow it. I was expecting an AV, or some sort of exception.