Student Student - 3 months ago 6
C++ Question

C++ Variable Confusion

I'm new to programming C++. Consider the following code:

int a;
a = 4;
int b = a;
b++;


At the end of this code,
a=4
and
b=5
. I would like it if
a=5
through modifying
b
. How do I modify my code so that this happens? Thanks!

Answer

For the code to do what you want, b would need to be a reference to a.

int a; a = 4; int &b = a; b++;

Comments