e******0 发帖数: 211 | 1 在看c++ faq++,有个问题
faq 31.04
#include
#include
using namespace std;
class X { };
void mayThrow() throw(int)
{ throw 42; }
class Fred {
public:
Fred() throw(bad_alloc, int);
~Fred() throw();
Fred(const Fred& f) throw();
Fred& operator= (const Fred& f) throw();
private:
X* p_;
};
Fred::Fred() throw(bad_alloc, int)
: p_(new X()) { mayThrow(); }
Fred::~Fred() throw()
{ cout << "Not reached #1\n"; delete p_; }
int main()
{
try {
Fred f;
cout << "Not reached #2\n";
}
catch (int e) {
cout << "Exception caught: " << e << "\n";
}
}
Fred 的ctor仍出exception, fred 出了 try{} 的作用域,会自动调用fred的析构函
数?
所以会释放 X.
但是输出是
Exception caught: 42
请问大家,我理解哪里错了 |
t****t 发帖数: 6806 | 2 dtor is not called for partially executed ctor.
【在 e******0 的大作中提到】 : 在看c++ faq++,有个问题 : faq 31.04 : #include : #include : using namespace std; : class X { }; : void mayThrow() throw(int) : { throw 42; } : class Fred { : public:
|
e******0 发帖数: 211 | 3 谢谢thrust
【在 t****t 的大作中提到】 : dtor is not called for partially executed ctor.
|
h***i 发帖数: 1970 | 4 thrust已经回答了你的问题, btw, 写C++ exception specifications绝对不是好的
style, 应该不写.
【在 e******0 的大作中提到】 : 在看c++ faq++,有个问题 : faq 31.04 : #include : #include : using namespace std; : class X { }; : void mayThrow() throw(int) : { throw 42; } : class Fred { : public:
|