I was trying some things with references and pointers, and I found something that I do not understand at all.
Here is my code:
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "con\n"; }
~A() { cout << "des\n"; }
void f() { cout << "bla" << endl; }
};
A &createA()
{
A *a = nullptr;
{
A b;
a = &b;
cout << *&a << endl;
}
return *a;
}
int main()
{
A &b(createA());
cout << &b << endl;
b.f();
system("pause");
return 0;
}
The output:
con
des
0058FE0B
0058FE0B
bla
Press any key to continue . . .
As you can see the member function f() still gets called, even after the object itself has been destroyed. Why? I thought there should be some error, but the function f() does get called and it event does everything correctly, why?