int x;
int& foo = x;
// foo is now a reference to x so this sets x to 56
foo = 56;
如何将
int& foo = x;
语句拆分为两个语句?通过拆分,我的意思是使用两个语句,如以下示例所示:
int y;
int* ptr = &y;
我可以将
int* ptr = &y
拆分为两个首先声明指针的语句。int* ptr;
ptr = &y; //then assigning the pointer to point to y
如何做类似引用的事情?我也在寻找关于为什么或为什么不做的解释?
最佳答案
你就是不行将引用视为类似于const
指针:int& foo = x
与int * const foo = &x
相同。
这就是为什么您不能重新分配引用或声明没有值的新引用的原因。
关于c++ - 在两个语句中创建引用变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31322845/