#include <iostream>
using namespace std;
class Test
{
public:
Test()
{
printf("construct ..\n");
}
~Test()
{
printf("destruct...\n");
}
};
Test Get()
{
Test t = Test();
return t;
}
int main(int argc, char *argv[])
{
Test t = Get();
return 0;
}
la salida de la consola es:¿Por qué se ha llamado al destructor una sola vez?
$ g++ -g -Wall -O0 testdestructor.cc
$ ./a.out
construcción ..
destrucción ...
posible duplicado de [¿Qué son elisión de copia y optimización del valor de retorno?] (Http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) –