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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:03:31+00:00 2026-06-08T19:03:31+00:00

Possible Duplicate: How to return member that can be changed? I learn that if

  • 0

Possible Duplicate:
How to return member that can be changed?

I learn that if i use in const& in the assignment (and in the called method signature) than the lifetime of the refereed object is extended until end of method.

Employee const& getEmp(int a) {
    return Employee(a);
}
Employee const& tmpEmp = m.getEmp(10);  //
... stuff 
//end of scope - tmpEmp valid until here

I wrote little program and saw that it work as expected.
My question is not how to do this?
My question is about how compiler do this?
As you see from the example the destructor is called immediately after return , so i wonder how is that the destructor called , but tmpEmp is valid after the desructor called ?

#include <iostream>
using namespace std;

class Employee {
public:
    Employee() : a(0){
        cout << "c-emp" << a << endl;
    }
    Employee(const Employee& newE) {
        a = newE.a;
        cout << "c-c-emp" << a << endl;
    }
    Employee(int a) : a(a) {
        cout << "c-emp" << a << endl;
    }
    ~Employee() {
        cout << "d-emp" << a << endl;
    }
    int a;
};
class Manager {
public:
    Manager() {}
    ~Manager() {}
    Employee const& getEmp(int a) {
        return Employee(a);
    }
};

int main(int argc, char **argv) {
    Manager m;
    Employee const& tmpEmp = m.getEmp(10);
    cout << "tmpEmp " << tmpEmp.a <<endl;
}

output:
c-emp10
d-emp10   - destructor is called and tmpEmp is still valid? how is that?
tmpEmp 10
  • 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-08T19:03:34+00:00Added an answer on June 8, 2026 at 7:03 pm

    You learned wrong.

    First, assignment never has any effect on the lifetime of an object.
    It’s just an operator with a side effect.

    Second, if you initialize (not assign) a const reference with a
    temporary, the lifetime of the temporary is extended to match the
    lifetime of the reference. There are exceptions, however. (And I’ve
    never found any pratical use for this feature.)

    A const reference used as a return value is one of the exceptions (for
    the simple reason that it’s not implementable). Initializing a return
    of a const reference type with a temporary does not extend the life of
    the temporary.

    And finally, even if it did, it wouldn’t help you in your case, because
    the reference which was initialized by the temporary ceases to exist
    after the full expression which invokes the function.

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

Sidebar

Related Questions

Possible Duplicate: Overloading by return type Why can't I declare three member functions like
Possible Duplicate: howto return a array in a c++ method? How can an array
Possible Duplicate: howto return a array in a c++ method? How an array can
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: Proper Use of yield return What is the use of the yield
Possible Duplicate: Can I create an automatic property (no private member) with get and
Possible Duplicate: Why can you return from a non-void function without returning a value
Possible Duplicate: Does a const reference class member prolong the life of a temporary?
Possible Duplicate: Declaring a function that return a 2D array in a header file?
Possible Duplicate: How do I initialize a member array with an initializer_list? You can

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.