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
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.