引自C++PRIMER FOURTH EDITION
① A noconst reference may be only attached to an object of the same type as the reference itself.
② A const reference may be bound to an object of a different type but related type or to an rvalue(right value : 右值)
我个人觉得第一句话每个人programmer理解起来都没有任何问题,但是第二句话会出现一个令人困惑的问题:
Question : "const reference could refer to a noconst object"
int m = 10 ;
const int &k = m
我们会有一样的疑问和不理解,疑问的根源是被const修饰后的variable是不能发生变化的。
那依据上面这条代码,我现在修改m的值,岂不是就改变了引用k的值,这与const自相矛盾...
代码和结果如下:
#include<iostream>
using namespace std ;
int main()
{
int i = 10 ;
int &j = i ;// j refers to i;
i = 11 ;// change the value of variable,i .
cout << "i = " <<