为什么常量引用的行为方式与常量指针不同,以便我可以实际更改它们指向的对象?它们真的看起来像是另一个简单的变量声明。我为什么要使用它们?
这是我运行的一个简短示例,它编译并运行没有错误:
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;
}
原文由 Bat0u89 发布,翻译遵循 CC BY-SA 4.0 许可协议
最明确的答案。 “X& const x”有意义吗?