Why can const int& bind to an int?

Viewed 3364

In the C++ primer, I found that const int & can bind with a int object.I don't understand that,because I think const int & should bind with a const int not a int object, the int object can change, the book explain this question for that when the const int & object bind with int; there is a temporary object between the two, for example:

int a=0;
const int &r=a;

We can use b as the temporary value, so above is equal that:

const int b=a;
const int &r=b;

But I think the book is not right, because if there is a temporary like b existing between a and r,the value of r can't be changed, but when I debug the following coding in visual studio, I found it is not right:

int a=0;
const int &r=a;
a=3;
cout<<r<<endl;

The output is that r=3; the value of r can be changed, why? I don't understand that.

8 Answers
Related