Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a local object from a function

Tags:

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