+ 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!
5 Answers
+ 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
+ 6
Thanks Scooby , got it now.
I've only used references in function calls before :-)
+ 5
cout << m << n;//finally print m and n value which is store at the time of initialization Adam Aksu
+ 4
welcome âș
0
Adam Aksu hello