If I write a factory method that instantiates an object locally and then returns by value, intending to take advantage of NRVO (as per some of the answers here: c++11 Return value optimization or move?), will a pointer/reference to the local object point to the object that is assigned the return value of the method?
Object ObjectBuilder::BuildObject( void )
{
Object obj;
//this->ObjectReference = obj; //Disregard this
//OR
this->ObjectPtr = &obj;
return obj;
}
In use:
ObjectBuilder builder;
Object newObject = builder.BuildObject();
Does builder.ObjectPtr refer to newObject?
ObjectBuilder
accesses it? – Oliveroliverathis->ObjectReference = obj;
is OK since it copies the object (assuming those variables are defined so that it compiles) – BurObjectReference
is of typestd::reference_wrapper<Object>
? – OliveroliveraObject&
can't be assigned, only constructed. I think that's what M.M was referring to – Oliverolivera