Returning a local object from a function

Posted by pocoa on Stack Overflow See other posts from Stack Overflow or by pocoa
Published on 2010-04-02T16:50:56Z Indexed on 2010/04/02 16:53 UTC
Read the original article Hit count: 244

Filed under:
|
|
|
|

Is this the right way to return an object from a function?

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));

I'm getting an error, "taking address of temporary". Should I use this way:

Car &getCar(string model, int year) {
   Car c(model, year);
   return c;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about object