I have a virtual method that returns a const reference to object. In one of derived classes I need to return a value. Is it possible to return a copy when using const reference return type?
Here is what I'm trying to do (simplified code because it is more complex):
const Object& method(){
Object object;
//...
return object; //Wrong, returning reference to local variable.
}
I tried to use static value in that way:
const Object& method(){
static Object object;
object = Object();
//...
return object;
}
It is easiest solution but not very elegant.