任务
找出如下代码的错误,编译代码的时候使用-fno-elide-constructors编译参数,分析构造函数(普通构造和拷贝构造)和析构函数调用的次数
#include <iostream>
using namespace std;
class Test{
public:
Test(int size){
cout << "Test(int size)" << endl;
data = new int[size];
}
Test(const Test obj){
cout << "Test(const Test obj)" << endl;
*this = obj;
}
~Test(void){
cout << "~Test()" << endl;
delete data;
}
private:
int *data;
};
Test function(Test obj)
{
Test tmp = obj;
return tmp;
}
int main(void)
{
Test obj1(3);
Test obj2 = function(obj1);
return 0;
}