When you want to return an instance from a method, do you create the object and send a pointer back, or a reference back? Whats the correct method and the method signature for this?
There are a lot of ways to do this in C++. Unfortunately most of them result in confusion on who is responsible for allocating and deallocating the object. There are two methods that I recommend:
// Return a real object, automatic stack allocation.
Foo GetFoo1()
{
Foo f;
// Init f.
return f;
}
// Use a smart, reference counted pointer that handles deallocation itself.
boost::shared_ptr<Foo> GetFoo2()
{
boost::shared_ptr<Foo> f(new Foo);
// Init f
return f;
}
The answer depends on what exactly you are doing and who is responsible for deallocating.
First method: allocate on the heap and return. Who ever called the function will be responsible for deleting the returned pointer.
SomeObject* constructObject ()
{
SomeObject* obj = new SomeObject ();
return obj;
}
Then in some other function
void functionThatNeedsObject ()
{
SomeObject* obj = constructObject ();
//You must delete obj when done
}
Second method: Return a reference. You must be careful not to go out of scope by returning local or temporary variables.
Dont do this:
int& DoubleValue(int nX)
{
int nValue = nX * 2;
return nValue; // return a reference to nValue here
} // nValue goes out of scope here
You can return references to member variables or variables passed as arguments to the function.
SomeStruct& RefFunction(SomeStruct& nX, SomeStruct& nY)
{
return nX;
} //nX is still in scope because it was passed to us
Either return by value (people incorrectly assume that this is slow) or, if you're returning an override of a polymorphic type, return an auto_ptr (or better a unique_ptr in C++0x).
The reason you do NOT use a shared_ptr is that you can never get your pointer out of it and use a different ownership semantic.
Never return a reference to a local instance.
If I'm creating an instance purely to return, I would return by value as first preference.
Only if the object type was not practically copyable would I consider returning via a smart pointer encapsulating the transfer of ownership.
Returning a reference I reserve for returning a reference to an object whose ownership isn't being transferred out of the function, that is it is already owned by something else and it's existence is guaranteed until a defined time after the function returns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With