简体   繁体   English

std::runtime_error 子项中的默认构造函数

[英]The default constructor in an child of std::runtime_error

I have two exception classes that inherit from std::runtime_error.我有两个从 std::runtime_error 继承的异常类。 The first one, AError , works just fine.第一个, AError ,工作得很好。

class AError : public std::runtime_error {
public:
    const char* what() const noexcept
    {
        return "Error of type A!";
    }
};

The second one, BError , does not compile because std::runtime_error does not have a default constructor.第二个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!";
    }
};

It seems to me that AError should not compile because the default constructor of AError should call the default constructor of std::runtime_error in the default constructor AError .在我看来, AError不应该编译,因为AError的默认构造AError应该在默认构造函数AError调用std::runtime_error的默认构造AError What is happening in the default constructor of AError that allows that example to compile?允许该示例编译的AError的默认构造函数中发生了什么?

It seems to me that AError should not compile because the default constructor of AError should call the default constructor of std::runtime_error in the default constructor AError.在我看来,AError 不应该编译,因为 AError 的默认构造函数应该在默认构造函数 AError 中调用 std::runtime_error 的默认构造函数。

The default ctor of AError is deleted because it inherits from a class that has no default ctor. AError的默认 ctor 被删除,因为它继承自一个没有默认 ctor 的类。 try the following and the code won't compile尝试以下操作,代码将无法编译

AError er;

The definition of AError compiles because it doesn't have a default constructor (one that accepts no arguments). AError的定义AError编译,因为它没有默认构造函数(不接受任何参数的构造函数)。 Implicit generation of the default constructor is suppressed because std::runtime_error doesn't have one.默认构造函数的隐式生成被抑制,因为std::runtime_error没有。 The class definition, in itself, has no diagnosable errors, since it doesn't create any instances of the class.类定义本身没有可诊断的错误,因为它不创建类的任何实例。

There is a diagnosable error on any attempt to create an instance of AError using a default constructor使用默认构造函数创建AError实例的任何尝试都会出现可诊断错误

AError an_error;    // error since AError cannot be default constructed

In BError , the definition of the constructor (within class BError )BError ,构造函数的定义(在类BError

 BError(int n) : num_(n) {}; // ERROR: no default constructor for runtime_error

attempts to implicitly construct the base std::runtime_error , even though it is not listed in the initialiser list.尝试隐式构造基础std::runtime_error ,即使它未在初始化列表中列出。 So this definition is functionally equivalent to所以这个定义在功能上等价于

BError(int n) : std::runtime_error(), num_(n) {};

and is a diagnosable error, due to the attempt - clearly visible to the compiler - to use the (non-existent) default constructor of std::runtime_error .并且是一个可诊断的错误,因为尝试使用std::runtime_error的(不存在的)默认构造函数 - 编译器清楚可见。

You can make this compile by simply using an appropriate constructor from std::runtime_error , such as您可以通过简单地使用std::runtime_error的适当构造函数来进行编译,例如

BError(int n) : std::runtime_error(""), num_(n) {};

which will call the constructor of std::runtime_error that accepts a const char * .它将调用接受const char *std::runtime_error的构造const char *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM