+ 2
Explain this code
#include <iostream> using namespace std; int x=15; int & f() { return x; } int main () { f()=10; cout<<x; }
3 Respuestas
+ 6
x is defined globally (declared outside main function)
f returns a reference of x, so one can change the value by assigning the reference (alias) a different value, here 10
You can think of this as a shortcut of these steps:
int& a = f();
a = 10;
// after that, both a and x are 10, since both describe the same spot in memory (reference)
0
Thank you Matthias
0
Thx Matthias & Akeel Mohamed 😃