由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教一个c++ throw exception 问题
相关主题
没有经过构造函数???请问关于c++实现singleton的问题?
我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。C++ 书推荐
C++的一个小疑问,求解惑急问:compile and build dependency
一个C++面试题分析find bugs of c++ codes
pointer to base class = new derived, what will happend??关于C/C++里的Static variable的memory allocation/initializa
两个继承问题C++设计疑问
one question about operator delete几个C++书写风格疑问
一道c++的考古题c++ exception
相关话题的讨论汇总
话题: fred话题: throw话题: exception话题: int话题: c++
进入Programming版参与讨论
1 (共1页)
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:

1 (共1页)
进入Programming版参与讨论
相关主题
c++ exceptionpointer to base class = new derived, what will happend??
C++弱问两个继承问题
C++问题one question about operator delete
回答C++的弱问题一道c++的考古题
没有经过构造函数???请问关于c++实现singleton的问题?
我这个C++程序有没有什么问题啊?请指点。。。谢谢。。。C++ 书推荐
C++的一个小疑问,求解惑急问:compile and build dependency
一个C++面试题分析find bugs of c++ codes
相关话题的讨论汇总
话题: fred话题: throw话题: exception话题: int话题: c++