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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:35:35+00:00 2026-05-28T15:35:35+00:00

This Question arises from a Q&A here I have some doubts that i think

  • 0

This Question arises from a Q&A here

I have some doubts that i think cant be asked in the followup comments there, and not too sure if i could have edited the linked question with my doubts…hence a new question.

Firstly,what i learnt from the linked question is that returning ref to local is EVIL!

Now,consider the same example from that question,a bit modified though:

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class A
{
    public:
        int x;
        string s;
        A(int xx,string ss):x(xx),s(ss)
    {
        cout<<"A::A()"<<x<<","<<s<<"\n";
    }
        A(const A& that)
        {
            cout<<"cpy ctr\n";
            x=that.x;
            s=that.s;
        }
        A& operator=(const A& that)
        {
            cout<<"operator=\n";
        }
        ~A()
        {
            cout<<"~A()"<<s<<"\n";
        }
};

const A& getA1()
{
    A a(1,"A1");
    cout<<"returning from A1\n";
    return a;
}

A& getA2()
{
    A a(2,"A2");
    cout<<"returning from A2\n";
    return a;
}

A getA3()
{
    A a(3,"A3");
    cout<<"returning from A3\n";
    return a;
}

int main()
{ 
    A &newA2 = getA2();       //.....................LINE 2
    cout<<"returned from A2\n";
    cout<<"-----------------------------\n";
    A newA3 = getA3();       //......................LINE 3
    //A const  newConstA3 = getA3 ();
    cout<<"returned from A3\n";
    cout<<"-----------------------------\n";
    //cout<<"newA2="<<newA2.x<<","<<newA2.s<<"\n";
    cout<<"newA3="<<newA3.x<<","<<newA3.s<<"\n";

}

The output is as follows..

A::A()2,A2
returning from A2
~A()A2
returned from A2
-----------------------------
A::A()3,A3
returning from A3
returned from A3
-----------------------------
newA3=3,A3
~A()A3

Now my doubts…

  1. in LINE 2, the fun getA2() (which returns by ref) returns after destructing the temporary,so the recieving object newA2 is dangerous to use.(the reason why i have comented out its use.) so then why doesent the fun getA3() destruct the temporary even though it returns by “copy” (Note that the temporary’s destructor inside getA3() doesnt get called ever..only newA3 object’s destructor gets called when it dies at the end of main() )? I have no clue where does that temp go??

  2. Now if in LINE 2 i change it from A& newA2 = getA2(); to A newA2 = getA2(); ,the copy ctr gets called here(which eventually gives a segfault as the referent has died),but why doesnt the copy ctr gets called at LINE 3 ? How does the state of the temporary is getting copied into object newA3,notice no operator=() is being called either ?

  3. Lastly.. the output here is from gcc, but does MSVC++2008 work differently??
    As even for the code A newA3 = getA3(); in LINE 3 it behaves the same as LINE 2,i.e, it destructs the temp before returning! any clues?

  • 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-28T15:35:35+00:00Added an answer on May 28, 2026 at 3:35 pm
    1. It appears you are benefiting from Return Value Optimisation.

    2. Because you are copying an, albeit invalid, reference into a new object.

    3. MSVC and GCC work broadly the same. Avoid undefined behaviour (This can be harder than it may seem) and if they don’t work the same way then there is a bug in the compiler.

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

Sidebar

Related Questions

This question arises from this question . One saying is that if a language
This question comes on the heels of the question asked here . The email
G'day, Edit: While this question covers a situation that can arise in programming a
This question is about removing sequences from an array, not duplicates in the strict
This question was very helpful, however I have a list control in my report,
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
What I currently have looks a bit like this: if(userLikesBananas) { return from fruit
So I know that there is a very similar question to this all over
This question arises out of a combination of this being my first time working
First, this is a very basic question that I'm unsure of how to phrase.

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.