B
bb
Hi,
Have a query regarding the life of temporaries. Here is the code...
class MyNumber {
public:
MyNumber(int n) : n(n) {
cout << "Object Constructed." << endl;
}
virtual ~MyNumber() {
cout << "Object Destructed." << endl;
}
MyNumber(const MyNumber& rhs) {
cout << "Object Copy Constructed." << endl;
n = rhs.getValue();
}
MyNumber& operator=(const MyNumber& rhs) {
if (this == &rhs) return *this;
cout << "Object Assigned." << endl;
n = rhs.getValue();
return *this;
}
int getValue() const {
return n;
}
private:
int n;
};
Main here...
-----------------
int main(int argc, char** argv) {
cout << "Understanding the life of temporaries..." << endl;
MyNumber mn1(9);
cout << "mn1: " << mn1.getValue() << endl;
MyNumber mn2 = mn1; // 'copy constructed' as expected
cout << "mn2: " << mn2.getValue() << endl;
cout << endl;
cout << "... Life of Temporaries ... case 1" << endl;
justReturn(mn1); // temporaries are 'destructed' on return as expected
cout << "... Life of Temporaries ... case 1" << endl;
cout << endl;
MyNumber mn3(0);
cout << endl;
cout << "... Life of Temporaries ... case 2" << endl;
mn3 = justReturn(mn1); // calls assignment operator as expected
cout << "... Life of Temporaries ... case 2" << endl;
cout << "mn3: " << mn3.getValue() << endl;
cout << endl;
cout << "... Life of Temporaries ... case 3" << endl;
MyNumber mn4 = justReturn(mn1); // not 'copy constructed', why?
cout << "... Life of Temporaries ... case 3" << endl;
cout << "mn4: " << mn4.getValue() << endl;
cout << endl;
cout << "Good bye" << endl;
}
// Receives the argument by value and returns the same by value
//
MyNumber justReturn(MyNumber mn) {
return mn;
}
The program output is as follows:
-------------------------------------------------
Understanding the life of temporaries...
Object Constructed.
mn1: 9
Object Copy Constructed.
mn2: 9
.... Life of Temporaries ... case 1
Object Copy Constructed.
Object Copy Constructed.
Object Destructed.
Object Destructed.
.... Life of Temporaries ... case 1
Object Constructed.
.... Life of Temporaries ... case 2
Object Copy Constructed.
Object Copy Constructed.
Object Assigned.
Object Destructed.
Object Destructed.
.... Life of Temporaries ... case 2
mn3: 9
.... Life of Temporaries ... case 3
Object Copy Constructed.
Object Copy Constructed.
Object Destructed.
.... Life of Temporaries ... case 3
mn4: 9
Good bye
Object Destructed.
Object Destructed.
Object Destructed.
Object Destructed.
-------- End of program output ----------
My questions:
-------------------
Am using gcc v4.0.2 on Redhat Fedora Core4 - 2.6.15
In case 3 above...
1. How is the object mn4 getting constructed? why there was no call to
the 'copy constructor'?
2. The 'returned' temporary is not destroyed immediately on return like
in other cases, why? it is actually getting destroyed much later at
the end of scope.
3. Is it due to some clever compiler optimization in gcc; kind of
aliasing happens between the returned temporary and mn4?
Thanks guys.
Have a query regarding the life of temporaries. Here is the code...
class MyNumber {
public:
MyNumber(int n) : n(n) {
cout << "Object Constructed." << endl;
}
virtual ~MyNumber() {
cout << "Object Destructed." << endl;
}
MyNumber(const MyNumber& rhs) {
cout << "Object Copy Constructed." << endl;
n = rhs.getValue();
}
MyNumber& operator=(const MyNumber& rhs) {
if (this == &rhs) return *this;
cout << "Object Assigned." << endl;
n = rhs.getValue();
return *this;
}
int getValue() const {
return n;
}
private:
int n;
};
Main here...
-----------------
int main(int argc, char** argv) {
cout << "Understanding the life of temporaries..." << endl;
MyNumber mn1(9);
cout << "mn1: " << mn1.getValue() << endl;
MyNumber mn2 = mn1; // 'copy constructed' as expected
cout << "mn2: " << mn2.getValue() << endl;
cout << endl;
cout << "... Life of Temporaries ... case 1" << endl;
justReturn(mn1); // temporaries are 'destructed' on return as expected
cout << "... Life of Temporaries ... case 1" << endl;
cout << endl;
MyNumber mn3(0);
cout << endl;
cout << "... Life of Temporaries ... case 2" << endl;
mn3 = justReturn(mn1); // calls assignment operator as expected
cout << "... Life of Temporaries ... case 2" << endl;
cout << "mn3: " << mn3.getValue() << endl;
cout << endl;
cout << "... Life of Temporaries ... case 3" << endl;
MyNumber mn4 = justReturn(mn1); // not 'copy constructed', why?
cout << "... Life of Temporaries ... case 3" << endl;
cout << "mn4: " << mn4.getValue() << endl;
cout << endl;
cout << "Good bye" << endl;
}
// Receives the argument by value and returns the same by value
//
MyNumber justReturn(MyNumber mn) {
return mn;
}
The program output is as follows:
-------------------------------------------------
Understanding the life of temporaries...
Object Constructed.
mn1: 9
Object Copy Constructed.
mn2: 9
.... Life of Temporaries ... case 1
Object Copy Constructed.
Object Copy Constructed.
Object Destructed.
Object Destructed.
.... Life of Temporaries ... case 1
Object Constructed.
.... Life of Temporaries ... case 2
Object Copy Constructed.
Object Copy Constructed.
Object Assigned.
Object Destructed.
Object Destructed.
.... Life of Temporaries ... case 2
mn3: 9
.... Life of Temporaries ... case 3
Object Copy Constructed.
Object Copy Constructed.
Object Destructed.
.... Life of Temporaries ... case 3
mn4: 9
Good bye
Object Destructed.
Object Destructed.
Object Destructed.
Object Destructed.
-------- End of program output ----------
My questions:
-------------------
Am using gcc v4.0.2 on Redhat Fedora Core4 - 2.6.15
In case 3 above...
1. How is the object mn4 getting constructed? why there was no call to
the 'copy constructor'?
2. The 'returned' temporary is not destroyed immediately on return like
in other cases, why? it is actually getting destroyed much later at
the end of scope.
3. Is it due to some clever compiler optimization in gcc; kind of
aliasing happens between the returned temporary and mn4?
Thanks guys.