+ 10

Can someone explain this C++ code?

I need an explanation on how the output of the code below can be 26. int m=2, n=6; int &x = m; int &y = n; m = x++; x = m++; n = y++; y = n++; cout << m << n; Thanks!

15th Jun 2018, 6:25 AM
Adam Aksu
Adam Aksu - avatar
5 ответов
+ 7
A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable. References vs Pointers References are often confused with pointers but three major differences between references and pointers are − 1)You cannot have NULL references. You must always be able to assume that a reference is connected to a legitimate piece of storage. 2)Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time. 3)A reference must be initialized when it is created. Pointers can be initialized at any time. int m=2, n=6; int &x = m;//assign m value to x reference int &y = n;//assign n value to y reference m = x++;//no change in value as rule 2 x = m++;//no change in value as rule 2 n = y++;//no change in value as rule 2 y = n++;//no change in value as rule
15th Jun 2018, 7:01 AM
Scooby
Scooby - avatar
+ 6
Thanks Scooby , got it now. I've only used references in function calls before :-)
15th Jun 2018, 7:16 AM
Adam Aksu
Adam Aksu - avatar
+ 5
cout << m << n;//finally print m and n value which is store at the time of initialization Adam Aksu
15th Jun 2018, 7:01 AM
Scooby
Scooby - avatar
+ 4
welcome ☺
15th Jun 2018, 7:17 AM
Scooby
Scooby - avatar
0
Adam Aksu hello
19th Jun 2018, 2:13 PM
yesse
yesse - avatar