class Test {
public:
Test() { OutputDebugStringA("Construct\n"); };
~Test() { OutputDebugStringA("Destruct\n"); };
Test(const Test& other) { OutputDebugStringA("Copy Construct\n"); }
Test& operator = (const Test& other) {
OutputDebugStringA("Copy Construct\n");
return *this;
}
Test GetNewTest() const {
OutputDebugStringA("GetNewTest\n");
return Test();
}
};
int main() {
Test test;
Test new_test = test.GetNewTest();
return 0;
}
Output is
Construct
GetNewTest
Construct
Destruct
Destruct
So I think return Test();
do not create a new Test
and copy to the return value. Is it defined in C++ standard or it's compiler optimizations behavior ? I'm using VS2015.