#include<iostream>
using namespace std;
//引用的本质就是一个指针常量
void func(int& ref)
{
ref = 100;
}
int main()
{
int a = 10;
//自动转换为int* const ref=&a;指针常量是指向不可修改,所以引用不可更改成为其他变量的别名
int& ref = a;
ref = 20;//内部发现ref是引用,自动帮我们转换为*ref=20;
cout << "a=" << a << endl;
cout << "ref=" << ref << endl;
func(a);
cout << "a=" << a << endl;
cout << "ref=" << ref << endl;
system("pause");
return 0;
}
网友评论