for example: http://codepad.org/yvEu6DqW
and just incase you don't like to click away, here's the sample code of the test i made:
#include <iostream>
using namespace std;
class Test{
public:
int m_ID;
Test Make(int ID){
return Test(ID);
}
Test operator + (Test &o){
return Test(m_ID+o.m_ID);
}
Test(int id) : m_ID(id){
cout << "Created Object " << m_ID << endl;
}
~Test(){
cout << "Destroyed Object " << m_ID << endl;
}
};
int main(int argc, char **argv){
Test A = Test(10);
Test B = A.Make(11);
Test C = A+B;
cout << "Object: "<<A.m_ID<<endl;
cout << "Object: "<<B.m_ID<<endl;
cout << "Object: "<<C.m_ID<<endl;
return 0;
}
essentially, from looking at the output, i read it as this:
Test A is created by declaring it, but then when i set it = to Test(10) it destroys the old object, and create's a new one in it's place.
then, i create B, again B being declared causes the constructor to be called, then immediately the destructor is called, after A.Make is even called, so the destroyed object contains the right value, but this extra code is still being called anyway.
I understand that many compiler's might compile out the return values so instead of storing a temp version to copy into the result, it simply copy's the result directly into the return value's location.
still, it's very confusing, and the memory overhead of what's going on is what scares me into only doing this sort of stuff via passing by reference, and using new/delete instead of ever using stack memory to hold the object.