What is the correct way to return an object from C ++?

I am confused between:

  • returns an object (but then the object is copied from a local variable to a function that consumes memory)
  • returns a pointer (but then you must remember to delete it, in the calling code, which is strange)
  • returns a link (but this is not possible, because it will be a reference to a local variable of the function, which will be deleted immediately after the function ends)

I am looking for the right way to return an object from a C ++ function to a basic C ++ (without library processing pointers and automatic memory deallocation). I want to know how this should be done.

thanks

+4
source share
8 answers

Modern compilers usually implement (Named) Optimization of the return value by which the copy that you reference (and logically expect) is not performed.

Since Visual Studio 2005 (VC ++ 8.0), I have not thought twice about returning objects.

+8
source

What about std::auto_ptr from <memory> ? Or if it comes to C ++ 0x std::unique_ptr ?

+3
source

A few empirical guidelines regarding returning objects from functions:

Returns per copy , unless

  • you return a non-local object (for example, a class member, a static variable, etc.) of the type that you pass to the per const reference function ; you can return this for const link
  • you return a non-local object , and callers should be able to invoke modification of the members of the returned object, thereby manipulating the object stored elsewhere; return this for the const link
  • you return a derived class to the hierarchy of polymorphic classes , users of the object should know only the base class and not use either 1 or 2; return this to smart pointer
+3
source

Assuming “no library processing and freeing up pointers automatically” means that there is no return by pointer, no boost::shared_ptr and no std::unique_ptr ( std::auto_ptr is evil anyway), you have two options:

Return by value:

 Foo bar(quux) { Foo foo; foo.frobnicate(quux); return foo; } Foo foo = bar("fred"); 

Follow this link:

 void bar(Foo& foo, quux) { foo.frobnicate(quux); } Foo foo; bar(foo, "fred"); 
+2
source

Depends on the "semantics" of the object. Values ​​must be returned by the copy, while entities must (or must, since they are not ideally copied) be returned by a pointer or reference.

If necessary, use links. But if you must return a pointer, using a smart pointer class such as std :: auto_ptr or boost :: shared_ptr is a good idea, because then the calling code does not need to think about freeing it when it is done with it.

+1
source

Return by value if you do not need subtype polymorphism. In the latter case, I would return auto_ptr<T> (C ++ 03) or unique_ptr<T> (C ++ 0x).

0
source
  • Return one of the smart pointer options (either it should depend on additional libraries, or wait until C ++ 1x)
  • Use link skip

Personally, I prefer the second option, because it is clear that the user needs to allocate and delete memory.

0
source

returns an object (but then the object is copied from a local variable to a function that consumes memory)

Optimal compilers may not take long to create a copy. You may also need to implement a copy constructor operator and an overload assignment operator depending on the contents of your object.

returns a pointer (but then you have do not forget to delete it, in the calling code, which is strange)

Yes, you must remember that you need to delete it, since you do not want to consider automatic cleaning for this issue.

returns a link (but this is not possible, because it will be a reference to a local variable of the function, which will be deleted as soon as the function ends)

The returned link is useful when you return this object (* this) from member functions. Otherwise, as you mentioned, it cannot be used.

Overall: it depends on your need, as described above, with respect to which one to choose.

0
source

All Articles