Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "return an object" in C++?

I know the title sounds familiar as there are many similar questions, but I'm asking for a different aspect of the problem (I know the difference between having things on the stack and putting them on the heap).

In Java I can always return references to "local" objects

public Thing calculateThing() {     Thing thing = new Thing();     // do calculations and modify thing     return thing; } 

In C++, to do something similar I have 2 options

(1) I can use references whenever I need to "return" an object

void calculateThing(Thing& thing) {     // do calculations and modify thing } 

Then use it like this

Thing thing; calculateThing(thing); 

(2) Or I can return a pointer to a dynamically allocated object

Thing* calculateThing() {     Thing* thing(new Thing());     // do calculations and modify thing     return thing; } 

Then use it like this

Thing* thing = calculateThing(); delete thing; 

Using the first approach I won't have to free memory manually, but to me it makes the code difficult to read. The problem with the second approach is, I'll have to remember to delete thing;, which doesn't look quite nice. I don't want to return a copied value because it's inefficient (I think), so here come the questions

  • Is there a third solution (that doesn't require copying the value)?
  • Is there any problem if I stick to the first solution?
  • When and why should I use the second solution?
like image 387
phunehehe Avatar asked Jul 28 '10 06:07

phunehehe


People also ask

How do you return an object?

When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function. To understand the concept of returning an object by value from a function, consider this example.

Can you return an object in C++?

Passing and Returning Objects in C++ In C++ we can pass class's objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so.

How do you return a class object in C++?

Example 2: C++ Return Object from a Function In this program, we have created a function createStudent() that returns an object of Student class. We have called createStudent() from the main() method. // Call function student1 = createStudent();

How do you return the address of an object in C++?

Method 1: Using Address-of or '&' operator This 'address-of' operator is a C++ mechanism that returns the address of the object when called with the object. It is a unary operator and the address returned by the operator is known a pointer as it points to the location of the object.


2 Answers

I don't want to return a copied value because it's inefficient

Prove it.

Look up RVO and NRVO, and in C++0x move-semantics. In most cases in C++03, an out parameter is just a good way to make your code ugly, and in C++0x you'd actually be hurting yourself by using an out parameter.

Just write clean code, return by value. If performance is a problem, profile it (stop guessing), and find what you can do to fix it. It likely won't be returning things from functions.


That said, if you're dead set on writing like that, you'd probably want to do the out parameter. It avoids dynamic memory allocation, which is safer and generally faster. It does require you have some way to construct the object prior to calling the function, which doesn't always make sense for all objects.

If you want to use dynamic allocation, the least that can be done is put it in a smart pointer. (This should be done all the time anyway) Then you don't worry about deleting anything, things are exception-safe, etc. The only problem is it's likely slower than returning by value anyway!

like image 61
GManNickG Avatar answered Sep 22 '22 20:09

GManNickG


Just create the object and return it

Thing calculateThing() {     Thing thing;     // do calculations and modify thing      return thing; } 

I think you'll do yourself a favor if you forget about optimization and just write readable code (you'll need to run a profiler later - but don't pre-optimize).

like image 27
Amir Rachum Avatar answered Sep 19 '22 20:09

Amir Rachum