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 = xint * const foo = &x相同。

这就是为什么您不能重新分配引用或声明没有值的新引用的原因。

关于c++ - 在两个语句中创建引用变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31322845/

10-15 05:53
查看更多