Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning an object in C++

Tags:

c++

c++11

I am learning C++ from a background of mostly C and Java and I am curious on what is the best way to return an object in C++ without having to copy the object. From my understanding, C++11 introduced rvalue references (&&) to move data from a temporary variable (as oppose to copying). Example:

std::string getStr(){
   return "Hello";
}

std::string &&hello = getStr();

Another way I could think of is using a shared pointer.

std::tr1::shared_ptr<std::string> getStr(){

    std::tr1::shared_ptr<std::string> hello(new std::string("Hello"));
    return hello;

}

auto hello = getStr();

I am thinking maybe an rvalue reference is better but I'd like a second opinion first before I use it. Which is better?

Also is it recommended that fields in a class be rvalue references if they won't be set using the constructor? Example:

class StringHolder{

   public:
     std::string &&str;
};

StringHolder sh;
sh.str = getStr();

Thank you guys very much!

like image 436
DanB91 Avatar asked Jul 08 '12 00:07

DanB91


1 Answers

This question will probably be closed as a duplicate. It is an oft-asked question. However I would like to answer it anyway.

In C++11, when you are the client of an object like std::string, you should pass it around by value and not worry so much about efficiency:

std::string getStr(){
   return "Hello";
}

std::string hello = getStr();

At this level you do not need to be concerned with rvalue references. Just know that std::string can "copy" from rvalues (such as the return from getStr()) very efficiently. This "copy" is actually called a "move" and is enabled automatically because you are copying from an rvalue (an anonymous temporary).

Don't try to optimize copying by reverting to reference counting. Only use reference counting if you need shared ownership semantics. This statement isn't always true. But for learning the basics, it is close enough that it is a good rule of thumb to follow.

You need to start worrying about rvalue references when you design your class that needs to be passed around by value:

class Widget
{
    // pointer to heap data
public:
    // ...
};

For a brief introduction to rvalue references and move semantics, N2027 is a decent tutorial. N2027 is too long to be pasted in here, but short enough to be an easy read. std::string follows the basics laid down in N2027 to enable you to pass it around by value guilt free. And you can follow those same design patterns in your Widget.

like image 183
Howard Hinnant Avatar answered Sep 23 '22 20:09

Howard Hinnant