在下面的代码中
using namespace std;
int main(){
int x=4;
int y=5;
cout << x <<endl;
int& foo = x;
// foo is now a reference to x so this sets x to 56
foo = 56; //reseting the reference foo
cout << x <<endl;
cout << foo <<endl;
foo= y; //this is supposed to be forbidden
cout << foo <<endl; // but I am getting foo=5
return 1;
}
Codeblocks编译正常,我在最后一个指令中得到foo = 5。对于我阅读的内容,您无法重新设置引用以使其引用另一个对象,所以我一直期待一个错误,这是怎么回事?