作 者:
tt_xtj () 等 級(jí):
信 譽(yù) 值: 100
所屬論壇: C/C++ C++ 語言
問題點(diǎn)數(shù): 20
回復(fù)次數(shù): 13
發(fā)表時(shí)間: 2006-10-12 13:17:05
在c++中,如果對(duì)象的構(gòu)造函數(shù)有異常被拋出,則該對(duì)象不會(huì)被構(gòu)造,對(duì)嗎?所
那么,構(gòu)造該對(duì)象所分配的空間由誰負(fù)責(zé)釋放(如果該對(duì)象是由new在堆中分配的呢)?
例如如下代碼:
#include <iostream>
#include <string>
using namespace std;
class test
{
public:
test(int m):n(m)
{
cout<<"Input:\n";
cin>>s;
if (s=="exception")
{
cout<<this<<endl<<sizeof(*this)<<endl;
cout<<&n<<endl;
throw s;
}
}
virtual ~test()
{
}
private:
int n;
string s;
int t;
};
void main()
{
test*ptrT;
try
{
ptrT=new test(6);
}
catch (string s)
{
cout<<s<<endl;
}
//delete ptrT;
test t2(8);
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。