+++++++++========返回一个临时变量的地址可以吗?==========++++++++++
我在一本C++的书上看到如下一段程序:
#include <iostream.h>
struct Person
{
char name[20];
unsigned long id;
float salary;
};
Person GetPerson()
{
Person temp;
cout << "Please enter a name for noe person:\n";
cin >> temp.name;
cout << "Please enter one's id number and his salary:\n";
cin >> temp.id >> temp.salary;
return temp; // ok? return a temp data ?
}
void Print(Person& p)
{
cout << p.name << " "
<< p.id << " "
<< p.salary <<endl;
}
void main()
{
Person employee[3];
for(int i = 0; i < 3;i++)
{
employee[i] = GetPerson();
Print(employee[i]);
}
}
各位大虾看看,到底返回一个临时变量的地址对不对?(在Person GetPerson()函数的最后一行:return temp);
如果是对的,临时变量在这个函数结束后就会释放的呀?