【发布时间】:2021-01-01 19:24:36
【问题描述】:
我有两个继承自 std::runtime_error 的异常类。第一个 AError 运行良好。
class AError : public std::runtime_error {
public:
const char* what() const noexcept
{
return "Error of type A!";
}
};
第二个BError 无法编译,因为std::runtime_error 没有默认构造函数。
class BError : public std::runtime_error {
public:
const int num_;
BError(int n) : num_(n) {}; // ERROR: no default constructor for runtime_error
const char* what() const noexcept
{
return "Error of type B!";
}
};
在我看来AError 不应该编译,因为AError 的默认构造函数应该在默认构造函数AError 中调用std::runtime_error 的默认构造函数。允许该示例编译的 AError 的默认构造函数中发生了什么?
【问题讨论】:
-
您尚未发布任何构造
AError类型对象的代码。请发布可用于测试您的问题的代码。 -
我犯了一个严重的错误。这篇文章应该被关闭。
-
@mana 然后,简单地删除它。
标签: c++ oop inheritance default-constructor