返回局部变量不能使用引用,因为局部变量在语句块结束后就会被销毁,找不到。这时候要使用拷贝。
比如:
Person read(istream &it)
{
Person thePerson;
it >> thePerson.name >> thePerson.address;
return thePerson;
}
返回的是一个对象,构造函数想在初始化的时候变成这个对象,那么就需要在拷贝的时候把自身等于这个对象。也就是*this = read(cin)
返回局部变量不能使用引用,因为局部变量在语句块结束后就会被销毁,找不到。这时候要使用拷贝。
比如:
Person read(istream &it)
{
Person thePerson;
it >> thePerson.name >> thePerson.address;
return thePerson;
}
返回的是一个对象,构造函数想在初始化的时候变成这个对象,那么就需要在拷贝的时候把自身等于这个对象。也就是*this = read(cin)