下面的代码是否表示内存泄漏?在C++类的构造函数中抛出异常
不调用Test类的析构函数(屏幕上没有输出),我假设所有分配给Int类数组的内存都不会返回给系统?我的假设是否正确?如果发生异常,我应该如何声明在构造函数中分配的资源?
#include <iostream>
using namespace std;
class Int{
public:
int v;
Int(){
cout<<"inside Int constructor ..."<<endl;
}
~Int(){
cout<<"inside Int destructor ..."<<endl;
}
};
class Test{
public:
Int* a;
Test(){
a=new Int[10];
cout<<"inside Test constructor ..."<<endl;
throw(0);
}
~Test(){
delete [] a;
cout<<"inside Test destructor ..."<<endl;
}
};
int main(){
try{
Test T;
}
catch (int e){
cout<<"Error!!!"<<endl;
}
return 0;
}