So in the following bit of code, I've been trying to figure out as to why the output of the code comes out to be...
X = 2 and Y = 2
when I originally thought it would be x = 1 and y = 1. I'm still a lil mind boggled with C++ coming into the semester and a little explanation with someone with more knowledge than me on this can hopefully mesh this concept into my head.
int main()
{
int x = 0;
int& y = x;
y++;
x++;
std::cout << "x = " << x << " y = " << y << std::endl;
}
key point is what the reference sign meant:
doing
will change the value inside that memory address, which is shared by both 'x' && 'y'. Same as the operation
As a result, you did 2 increment on the same memory address with intial value of '0', which the value becomes '2'.