为什么常量引用的行为方式与常量指针不同,这样我就可以真正改变它们所指向的对象?它们看起来真的像是另一个普通的变量声明。我为什么要用它们?
这是我运行的一个简短示例,它编译并运行时没有错误:
int main (){
int i=0;
int y=1;
int&const icr=i;
icr=y; // Can change the object it is pointing to so it's not like a const pointer...
icr=99; // Can assign another value but the value is not assigned to y...
int x=9;
icr=x;
cout<<"icr: "<<icr<<", y:"<<y<<endl;
}