r********e 发帖数: 27 | 1 对返回值引用不太熟,上网查了查,也不是很明白有段程序如下,引用hours指向local
var h, 是不是应该函数 GetWeeklyHours()返回,hours不指向任何变量(释放了),
所以应该程序出问题吧。但是我试了,是work的,哪位xd给解释一下。多谢了。
#include
using namespace std;
double & GetWeeklyHours()
{
double h = 46.50;
double &hours = h;
return hours;
}
int main()
{
double &hours = GetWeeklyHours();
cout << "Weekly Hours: " << hours << endl;
return 0;
} | X****r 发帖数: 3557 | 2 The behavior of this piece of code is undefined, which means
it can be anything, including the "right" thing.
local
【在 r********e 的大作中提到】 : 对返回值引用不太熟,上网查了查,也不是很明白有段程序如下,引用hours指向local : var h, 是不是应该函数 GetWeeklyHours()返回,hours不指向任何变量(释放了), : 所以应该程序出问题吧。但是我试了,是work的,哪位xd给解释一下。多谢了。 : #include : using namespace std; : double & GetWeeklyHours() : { : double h = 46.50; : double &hours = h; : return hours;
| r********e 发帖数: 27 | 3 谢了,知道了,大概是那块释放了的内存没被用掉,所以是对的 |
|