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

  • Home
  • SEARCH
  • 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 8025383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:08:53+00:00 2026-06-04T23:08:53+00:00

Can someone tell why test(2) object is destroyed after test_method() call? #include<iostream> #include<string> using

  • 0

Can someone tell why test(2) object is destroyed after test_method() call?

#include<iostream>
#include<string>

using namespace std;

class test
{
    int n;

    public:
    test(int n) : n(n)
    {
        cout << "test: " << n << endl;
    }

    ~test()
    {
        cout << "~test: " << n << endl;
    }

    test & test_method()
    {
        cout << "test_method: " << n << endl;
        return *this;
    }
};

int main(int argc, const char *argv[])
{
    cout << "main start" << endl;
    const test &test1 = test(1);
    const test &test2 = test(2).test_method();
    cout << "main end" << endl;
}

Output is:

main start
test: 1
test: 2
test_method: 2

~test: 2
main end
~test: 1
  • 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-04T23:08:54+00:00Added an answer on June 4, 2026 at 11:08 pm

    test(2).test_method() returns a reference, which is bound to test2, and then the object to which it refers is destroyed at the end of the full expression, since it is a temporary object. That should not be a surprise.

    The real surprise is that test1 remains a valid reference, because it is directly bound to a temporary, and binding a temporary to a reference extends the lifetime of the temporary to that of the reference variable.

    You only have to note that in the test(2) case, the temporary object isn’t bound to anything. It’s just used to invoke some member function, and then its job is done. It doesn’t “babysit” member functions, or in other words, lifetime extension isn’t transitive through all possible future references.


    Here’s a simple thought experiment why it would be impossible to actually have “arbitrary lifetime extension”:

    extern T & get_ref(T &);
    
    {
        T const & x = get_ref(T());
    
        // stuff
    
        // Is x still valid?
    }
    

    We have no idea if x remains valid beyond the first line. get_ref could be doing anything. If it’s implemented as T & get_ref(T & x) { return x; }, we might hope for magic, but it could also be this:

    namespace { T global; }
    T & get_ref(T & unused) { return global; }
    

    It’s impossible to decide within the original translation unit whether anything needs to be extended or not. So the way the standard has it at present is that it’s an entirely trivial, local decision, just made when looking at the reference declaration expression, what the lifetime of the temporary object in question should be.

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

Sidebar

Related Questions

Can someone tell me why this unit test that checks for exceptions fails? Obviously
Can someone tell me why when I cast a string of say 00332 I
Can someone tell me whether MSMQ (using transactions) supports competing consumers? Basically, I have
Can someone tell me how to accomplish this mapping using Fluent NHibernate? It's simply
Can someone please tell me how to hit test the scrollbars of a scrollviewer
Can someone tell me what is wrong with the following code? Set<String> cmds =
Can someone tell me how exactly works test ICMP? (One of methods to detect
Can someone tell me why this processes all the files and then does it
Can someone tell me how I can make a table be 100% height in
Can someone tell me the difference between: $(document).ready(function() { }); and: var someVar =

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.