#include<iostream>
using namespace std;
int & test01()
{
int a = 10;
return a;
}
int& test02()
{
static int a = 10;
return a;
}
int main()
{
// int& ref = test01(); //此函数返回的局部变量,函数执行完后释放,所以第一次cout是10,第二次cout就是乱码
// cout << "ref的值为:" << ref << endl;
// cout << "ref的值为:" << ref << endl;
int& ref = test02(); //此函数返回的静态(全局)变量,函数执行完后释放,所以无数次cout都是a的值
cout << "ref的值为:" << ref << endl;
cout << "ref的值为:" << ref << endl;
test02() = 1000; //函数做左值(出现在赋值语句左侧的表达式)的时候,必须要返回引用
//这里我们修改的是函数,所以必须要把引用的函数返回来(也就是a本身),值传递做不到,给的是副本,就不行
system("pause");
return 0;
}