下面的代碼是否表示內存泄漏?在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;
}