Return pointers or links from C ++ functions

If you want to return an instance from a method, you create an object and send the pointer back or the link back? What is the correct method and method signature for this?

+7
c ++ pointers reference
source share
6 answers

In C ++, there are many ways to do this. Unfortunately, most of them lead to confusion about who is responsible for the allocation and release of 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; } 
+8
source share

Go back by value (people mistakenly assume this is slow), or if you return an override of a polymorphic type, return auto_ptr (or better unique_ptr in C ++ 0x).

The reason you DO NOT use shared_ptr is because you can never extract your pointer from it and use different property semantics.

Never return a link to a local instance.

+6
source share

The answer depends on what you are doing and who is responsible for the release.

The first method: select in a heap and return. Anyone who has ever called this 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 the link. You must be careful not to go out of scope by returning local or temporary variables.

Do not 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 
+6
source share

If I create an instance that is for return only, I will return by value as the first preference.

Only if the type of the object was not practically copied, I would think about returning using an intelligent pointer encapsulating the transfer of ownership.

Returning the link that I reserve to return a reference to an object whose property is not transferred from the function, that is, it already belongs to something else, and its existence is guaranteed until a certain time after the function returns.

+4
source share

If you reference something like Factory Method , you will usually return a pointer. Better still, return the smart pointer and you will not create more leaks due to the use of the raw pointer.

Example:

 std::auto_ptr<Gizmo> MyFactory::CreateGizmo() { return new Gizmo; } 
+2
source share

It really depends on the size of your instance, which controls the lifetime of the instance. If this is a local instance, you can return by value, but incur the cost of creating and destroying the object twice (if you are not using RVO ). Another option is to return the pointer by building the object on the heap inside your function. However, with this approach, the client will be responsible for deleting the allocated memory and will always be subject to a memory leak. That is why you will need to use some kind of smart pointer. Anders Abel's code clearly illustrates the above two approaches with code examples. BTW, you cannot return a link to a local instance, since the instance will go out of scope after the function finishes.

0
source share

All Articles