lundi 22 mai 2017

in C++,why const in & can bind with a int object?

in the C++ primer,I found that const int & can bind with a int object.I don't understand that,because I think const in & 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.

Aucun commentaire:

Enregistrer un commentaire