Why does changing what a reference points to not throw an error?
问题 Iv got to the stage in my c++ study concerning references. It states the following rule: Once a reference is initialized to an object, it cannot be changed to refer to another object. Iv wrote a short code (as asked to in an exercise) that is meant to prove this rule correct. int y = 7; int z = 8; int&r = y; r = z; Can someone explain why this code compiles without any errors or warnings? 回答1: r = z does not change what r "points to." It assigns the value of z to the object pointed to by r .