A reference is just like addressing the original item. Like *(& some_variable)
In your code therefore the line
r2 = r1;
sets the value of d
to the value of i
which is 0
.
from then on all values are 0
if you replace the names r1
with ref_i
and r2
to ref_d
it will make sense.
Try it here:
int i = 0, &ref_i = i;
double d = 1, &ref_d = d;
ref_d = ref_i; // <<-------------------- Put value of i into d
std::cout << ref_d;
i = ref_d;
std::cout << i;
ref_i = d;
std::cout << ref_i;