#include <iostream>
using std::cout;
using std::endl;
float temp;
float fun1(float r)
{
temp = 3.14*r*r;
return temp;
}
float&fun2(float r)
{
temp = 3.14*r*r;
return temp;
}
void main()
{
float a = fun1(5.0);
//float &b = fun1(5.0);//该句不能通过编译。
float c = fun2(5.0);
float &d = fun2(5.0);
cout << a << endl;
//cout << b << endl;
cout << c << endl;
cout << d << endl;
using std::cout;
using std::endl;
float temp;
float fun1(float r)
{
temp = 3.14*r*r;
return temp;
}
float&fun2(float r)
{
temp = 3.14*r*r;
return temp;
}
void main()
{
float a = fun1(5.0);
//float &b = fun1(5.0);//该句不能通过编译。
float c = fun2(5.0);
float &d = fun2(5.0);
cout << a << endl;
//cout << b << endl;
cout << c << endl;
cout << d << endl;
}
之所以不能通过编译,原理是编译时B和函数返回值绑定,但是fun1()结束时会清理函数栈结构,B是在MAIN()的栈结构中,还没有结束,又因为引用是采用指针方式实现的,所以FUN1()结束时,指针被清理,但是B还没有清理。造成混乱。