Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:18:59+00:00 2026-06-07T07:18:59+00:00

I am learning C++ from a background of mostly C and Java and I

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:19:01+00:00Added an answer on June 7, 2026 at 7:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning Scala (coming from a background of mostly Java). I am trying
I've recently been learning vb.net, coming from a background in java and c. I'm
I am from a Java background and is learning C++. I encountered the following
G'Day Programmers, I am from Java background however I have just started learning C++
I'm learning Haskell from a Java background. When I program Java, I feel like
I'm learning C++ coming from a Java background (knowing a little C from many
I'm just learning c++ coming from a Java background. Just playing around with simple
I am fairly new to learning C# (from Java & C++ background) and I
I come from a Java background and I am learning Objective C. I am
learning python, coming from a php background. Keeping it short. Is there a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.