我到处都读过必须初始化引用然后再进行初始化.
为了测试我的理解,我写了以下小程序.似乎我已经成功地重新分配了一个引用.有人可以向我解释我的程序中实际发生了什么吗?
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
int main()
{
int i = 5, j = 9;
int &ri = i;
cout << " ri is : " << ri <<"\n";
i = 10;
cout << " ri is : " << ri << "\n";
ri = j; // >>> Is this not reassigning the reference? <<<
cout << " ri is : " << ri <<"\n";
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码编译正常,输出正如我所期望的那样:
ri is : 5 …Run Code Online (Sandbox Code Playgroud)