【发布时间】:2013-08-27 07:53:06
【问题描述】:
如果注释了line 8,则以下程序无法在 g++ 4.4 中编译。为什么?似乎当我重写 std::exception 构造函数时,我也必须重写它的析构函数。这是什么原因?
#include<iostream>
#include<exception>
using namespace std;
class A : public exception {
public:
A(string msg) : _msg(msg) {}
//~A() throw(){}; // line 8
const char* what() const throw() { return _msg.c_str();}
private:
string _msg;
};
int main()
{
}
编译错误是:
error: looser throw specifier for ‘virtual A::~A()’
【问题讨论】:
-
编译器是否报错?如果是这样,为什么不在问题中?
-
@DavidHeffernan,thanx,已修复
-
这是他的问题在底部(虽然可能是在编辑之后)。当然与 4.8.1 相同,因为默认的析构函数不会有此处请求的 throw 说明符。
-
@Laszlo 是的,它是在编辑并添加错误后的问题中
标签: c++ exception exception-handling destructor throw