#include<iostream>
using namespace std;
int &get1()
{
static int a = 10;
return a;
}
int &get2()
{
int b = 11;
return b;
}
int &get3()
{
int *c = new int;
*c = 10;
return *c;
}
//说局部变量是不能作为引用返回的
int main()
{
int a1, b1,c1;
a1 = get1();
b1 = get2();
c1 = get3();
get1() = 100;
get2() = 200;//这样子是不对的,因为b已经释放了。
get3() = 300;//不是静态的就会释放,根本不能改变它的值
cout << a1 << endl << b1 << endl << get1() << endl << get2() << endl;
cout << c1 << endl << get3() << endl;
system("pause");
}
非静态局部变量不能当左值的,这样即便运算出结果不报错误也是不合理的,因为这里的局部变量已经调用就会释放。