//引用必须初始化
void test02()
{
//int &a; //error,应用必须初始化
int a = 10;
int &b = a; //应用初始化后,不可以再指向另一块内存空间
int c = 20;
b = c; //赋值!!!只是把c的值20赋值给了b
cout << "a = " << a << endl; //a = 20
cout << "b = " << b << endl; //b = 20
cout << "c = " << c << endl; //c = 20
cout << "-----------------------" << endl;
cout << "&a = " << &a << endl; //&a = 00EFFC44
cout << "&b = " << &b << endl; //&b = 00EFFC44
cout << "&c = " << &c << endl; //&c = 00EFFC2C
cout << "-----------------------" << endl;
b = 4;
cout << "a = " << a << endl; //a = 4
cout << "b = " << b << endl; //b = 4
cout << "c = " << c << endl; //c = 20
}
引用一经初始化,他的指向就修改不了了
于 2022-06-18 10:42:58 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)