I have classes like these:
class Base
{
virtual const Room &getRoom() const = 0;
};
class Owner : public Base
{
const Room &getRoom() const override
{
return myRoom;
}
Room myRoom;
};
class Renter : public Base
{
const Room &getRoom() const override
{
return Room(); //this will not work
}
};
void main()
{
const auto& room = ownerOrRenter.getRoom();
}
This does not work because I am returning the reference of a local variable. But if I change the return type of Base::getRoom() to const Room, I will have to make a copy of Owner::myRoom every time getRoom() is called. What can I do?
Aucun commentaire:
Enregistrer un commentaire