int& fun(){
int a = 10;
cout << &a << endl;
return a;
}
int main() {
int& r = fun();
cout << r << endl;
cout << &r << endl;
return 0;
}
outputs of the code above are:
0xbf8567b4
10
0xbf8567b4
and when i change int& r = fun();
to int r = fun();
the addresses differ, they become:
0xbfa831e4
10
0xbfa83210
why this happens?
and i have another question that why i still can get "10" printed, "r" is the reference of the local variable "a" in fun(), isn't it that the "a" variable gets destroyed and the reference "r" becomes referencing nothing when the function calling finishes?
any help will be greatly appreciated!