Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning object from function

I am really confused now on how and which method to use to return object from a function. I want some feedback on the solutions for the given requirements.

Scenario A: The returned object is to be stored in a variable which need not be modified during its lifetime. Thus,

const Foo SomeClass::GetFoo() {
 return Foo(); 
}

invoked as:

someMethod() {
 const Foo& l_Foo = someClassPInstance->GetFoo();
//...
}

Scneraio B: The returned object is to be stored in a variable which will be modified during its lifetime. Thus,

void SomeClass::GetFoo(Foo& a_Foo_ref) {
     a_Foo_ref = Foo(); 
    }

invoked as:

someMethod() {
 Foo l_Foo;
 someClassPInstance->GetFoo(l_Foo);
//...
}

I have one question here: Lets say that Foo cannot have a default constructor. Then how would you deal with that in this situation, since we cant write this anymore:

Foo l_Foo

Scenario C:

Foo SomeClass::GetFoo() {
 return Foo(); 
}

invoked as:

someMethod() {
 Foo l_Foo = someClassPInstance->GetFoo();
//...
}

I think this is not the recommended approach since it would incur constructing extra temporaries.

What do you think ? Also, do you recommend a better way to handle this instead ?

like image 457
brainydexter Avatar asked Apr 11 '10 05:04

brainydexter


People also ask

How do you return an object from a function in C++?

Syntax: object = return object_name; Example: In the above example we can see that the add function does not return any value since its return-type is void. In the following program the add function returns an object of type 'Example'(i.e., class name) whose value is stored in E3.

Can a python function return an object?

Functions and methods can return objects. This is actually nothing new since everything in Python is an object and we have been returning values for quite some time.

Can a function return object to another function?

We must use a return keyword to return this object and return the obj created. We must create a variable myObj and then call the returnObj function. After we call this function, whatever the function will return (in this case, an object) will be stored inside the myObj variable.

Can we return object from JavaScript function?

Summary. JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.


1 Answers

First, let's look into the things that come into play here:

(a) Extending lifetime of a temporary when it's used to initialize a reference - I learnt about it in this publication by Andrei Anexandrescu. Again, it feels weird but useful:

class Foo { ... }

Foo GetFoo() { return Foo(); }  // returning temporary

void UseGetFoo()
{
   Foo const & foo = GetFoo();
   // ... rock'n'roll ...
   foo.StillHere();
}

The rule says that when a reference is initialized with a temporary, the temporary's lifetime is extended until the reference goes out of scope. (this reply quotes the canon)

(b) Return Value Optimization - (wikipedia) - the two copies local --> return value --> local may be omitted under circumstances. That's a surprising rule, as it allows the compiler to change the observable behavior, but useful.

There you have it. C++ - weird but useful.


So looking at your scenarios

Scenario A: you are returning a temporary, and bind it to a reference - the temporary's lifetime is extended to the lifetime of l_Foo.

Note that this wouldn't work if GetFoo would return a reference rather than a temporary.

Scenario B: Works, except that it forces a Construct-Construct-Copy-Cycle (which may be much more expensive than single construct), and the problem you mention about requiring a default constructor.

I wouldn't use that pattern to create a object - only to mutate an existing one.

Scenario C: The copies of temporaries can be omitted by the compiler (as of RVO rule). There is unfortunately no guarantee - but modern compilers do implement RVO.

Rvalue references in C++ 0x allows Foo to implement a resource pilfering constructor that not only guarantees supression of the copies, but comes in handy in other scenarios as well.

(I doubt that there's a compiler that implements rvalue references but not RVO. However there are scenarios where RVO can't kick in.)


A question like this requires mentioning smart pointers, such as shared_ptr and unique_ptr (the latter being a "safe" auto_ptr). They are also in C++ 0x. They provide an alternate pattern for functions creating objects.


like image 60
peterchen Avatar answered Oct 19 '22 18:10

peterchen