Reputation: 11
I have been struggling to find out how increment operators relate to the value of b, c and the way reference sign changes the value when it is being written. The program prints out 8, but I am not sure what the reference actually does in this particular situation. Here is a code:
int fun1(int d) {
++d;
return d++;
}
int fun2(int &d) {
++d;
return d++;
}
int main(void) {
int a = 1, b, c;
b = fun1(a);
c = fun2(b);
cout << a + b + c << endl;
return 0;
}
Thank you in advance!
Upvotes: 0
Views: 104
Reputation: 1000
Since the first answer didn't fully go into the example at hand, I'll elaborate on this, because it's, at first, a tricky subject IMO.
int a = 1, b, c;
This declares three int variables: a
, b
and c
. It also initializes a
with the value 1. b
and c
receive the default start value for ints, so 0.
As the first answer mentioned, fun1(int d)
copies the value of the variable passed to it into a local variable, but does not alter the original variable. What's the matter with ++d
and d++
, though? This got to do with the order in which operations get performed. ++d
takes the value of d
, increases it by one, then assigns it back to d
. d++
on the other hand first assigns the value of d
back to d
(thus returning it from a function if used in a return statement) before increasing it by one. Why is this relevant?
Let's keep in mind fun1(a)
works with a local variable so does not, in fact, alter variable a
. ++d
increases the local variable by 1, so it's now 2. Then d++
returns the current value of d
(2), and then increases the local variable by 1 again -- but this doesnt't get stored anywhere outside of the function fun1()
. Hence, the variable b
has now the value 2.
c = fun2(b);
is a different case, though. Here, we pass a reference to the variable b into the function. So ++d
doesn't just increase the value of a local variable but instead increases the value of b
, it's now 3. d++
returns this value to c (so it gets assigned 3), but also increases b
by 1 again to 4. Why? Because d
is a reference to b
.
Our ultimate values are hence: a = 1
, b = 4
, c = 3
, and 1 + 4 + 3 = 8.
Upvotes: 0
Reputation: 3265
The crux of your problem lie in this part of your code:
int fun1(int d) { }
int fun2(int &d) { }
The int d
in the first function are value semantics. This means that the value of the variable passed to function fun1
is copied and only know inside of fun1
.
The int &d
are reference semantics which means that the variable passed to fun2
is referenced by d
and as such if you modify d
inside of fun2
the actual value of the variable passed to fun2
is changed.
In your case this means that the value of a
is copied by fun1
while fun2
updates the value of b
. Because d
is used in fun2
as a reference to the actual variable b
wherein the value is stored.
And more to the point of the value of 8
printed by this program. ++d
is the pre-increment operator, d++
is the post-increment operator. Meaning:
d
first then use the new value of d
d
and use that to do something and after that increment the value of d
Combining these things (value/reference semantics and the behaviour of pre/post increment) we get that that fun1
and fun2
return the original value of d
which is incremented before the original value is returned. So return d++;
is equivalent to this piece of code:
int returnvalue = d;
d = d + 1;
return returnvalue;
But because fun2
has a reference d
to the variable b
in main, the value in b
is incremented by one because d
was post-incremented in return d++
.
ISOcpp value and reference semantics. And stackoverflow about the difference between pointers and references in C++.
If you need a much larger explanation of these terms and what they mean, this list of book recommendations is great. My choice would be A Tour of C++ by the creator of C++. It does a great job of going over everything you should now about C++ and is very cheap.
Upvotes: 4