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 717975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:28:25+00:00 2026-05-14T05:28:25+00:00

I am really confused now on how and which method to use to return

  • 0

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 ?

  • 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-05-14T05:28:26+00:00Added an answer on May 14, 2026 at 5:28 am

    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.


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

Sidebar

Related Questions

I'm trying to use regular expression right now and I'm really confuse. I want
I'm really confused. I'm trying to use some of the very simple Facebook plugins
Really confused why the QR output using RcppArmadillo is different than QR output from
I am really confused about object relationships! I have two classes Person and Address.
I am really confused right now and I can't get any right answers anywhere.
I am really wondering if jQuery remove function really remove elements from DOM. First,
I have an activity which displays some data fetched from the server. If no
I'm really confused with the program flow for how the paintComponent function works within
I encounter really weird problem on which I stuck. I am working on some
I'm really confused. I have a KML feed at https://views.scraperwiki.com/run/hackney_council_planning_kml_output/ ? ...Which worked perfectly

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.