Returning a reference to a local or temporary variable [duplicate]

You got lucky. Returning from the function doesn’t immediately wipe the stack frame you just exited.

BTW, how did you confirm that you got a 6 back? The expression std::cout << &i ... prints the address of i, not its value.

Leave a Comment