关于构造函数中的异常处理

来源:百度文库 编辑:神马文学网 时间:2024/10/04 06:13:10

作  者:tt_xtj ()
等  级:
信 誉 值: 100
所属论坛: C/C++ C++ 语言
问题点数: 20
回复次数: 13
发表时间: 2006-10-12 13:17:05
在c++中,如果对象的构造函数有异常被抛出,则该对象不会被构造,对吗?所
那么,构造该对象所分配的空间由谁负责释放(如果该对象是由new在堆中分配的呢)?
例如如下代码:
#include
#include
using namespace std;
class test
{
public:
test(int m):n(m)
{
cout<<"Input:\n";
cin>>s;
if (s=="exception")
{
cout<cout<<&n<throw s;
}
}
virtual ~test()
{
}
private:
int n;
string s;
int t;
};
void main()
{
test*ptrT;
try
{
ptrT=new test(6);
}
catch (string s)
{
cout<}
//delete ptrT;
test t2(8);
}