I am working on const-correctness of my code and just wondered why this code compiles:
class X
{
int x;
int& y;
public:
X(int& _y):y(_y)
Because you are not changing any variable in X
. Actually, you are changing _y
which is an outsider with respect to your class. Don't forget that:
y = newY;
Is assigning the value of newY
to the variable pointed by y
, but not the references them selves. Only on initialization the references are considered.