This happens when compiling the following code in Visual Studio 2010. My question is: the C ++ compiler will warn if the function returns the address of a local variable, but why doesn't it warn when returning a local link to a local variable?
Is it still wrong (returning a local reference to a local variable), but just that the compiler could not detect it? Examining the addresses "num" and "r" shows that they have the same memory location.
#include <iostream>
using namespace std;
int & intReference() {
int num = 5;
int &r = num;
cout << "\nAddress of num: " << #
return r;
}
void main() {
int &k = intReference();
cout << "\nk = " << k;
cout << "\nAddress of k: " << &k;
char c;
cin.get(c);
}
source
share