【发布时间】:2011-02-03 19:18:48
【问题描述】:
这是从函数返回对象的正确方法吗?
Car getCar(string model, int year) {
Car c(model, year);
return c;
}
void displayCar(Car &car) {
cout << car.getModel() << ", " << car.getYear() << endl;
}
displayCar(getCar("Honda", 1999));
我收到一个错误,“获取临时地址”。我应该这样使用吗:
Car &getCar(string model, int year) {
Car c(model, year);
return c;
}
【问题讨论】:
标签: c++ memory function object reference