I already know that the variable is destroyed when the program gets out of its scope. However, this did not seem to happen when I tried the code in the following snippet:
int& somethin()
{
int x1 = 4;
return x1;
}
int main() {
int x11;
x11 = somethin();
cout << x11 << endl;
return 0;
}
Surprisingly, the output is: 4
While when I declare "x11" as a reference variable, I get garbage value.
Any explanation?
Note: I repeated this test many times. I do not think it is about luck. Note: There is one who asked the same question before in StackOverflow, the answers were about the tester was lucky.