I am having some trouble understanding the behaviour in this snippet:
unsigned int i = 2;
const int &r = i;
std::cout << r << "\n";
i = 100;
std::cout << r << "\n";
The first print statement gives 2 as I expect, but when I change the value of the referenced variable, it is not reflected in the reference. The second print statement also gives 2, but I think it should give 100?
If I make variable i
into type int
instead of unsigned int
, it works as I expect. What is going on here?