一个相当理论上的问题......为什么常量引用不像常量指针那样运行,我实际上可以改变它们指向的对象?他们看起来像另一个简单的变量声明。为什么我会使用它们?这是我运行的编译和运行没有错误很短的例子:什么是常量引用? (不是对常量的引用)
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;
}
看'之前和之后'ICR = Y i';'。 –
一个不变的参考是...废话。 :) – jalf
这是否甚至编译? –