gpt4 book ai didi

c++ - C++ 中更宽松的抛出说明符

转载 作者:太空狗 更新时间:2023-10-29 23:10:59 26 4
gpt4 key购买 nike

这个错误是什么意思?我该如何解决?这是导致它的 header 代码:

class BadJumbleException : public exception {
public:
BadJumbleException (const string& msg); // Constructor, accepts a string as the message
string& what(); // Returns the message string
private:
string message; // Stores the exception message
};

这是源代码:

BadJumbleException::BadJumbleException (const string& m) : message(m) {}
string& BadJumbleException::what() { return message; }

编辑:这是错误:

looser throw specifier for 'virtual BadJumbleException::~BadJumbleException()

最佳答案

在 C++03 中,根据 §18.6.1/5,std::exception 有一个析构函数,该析构函数被声明为不会从中抛出任何异常 (a compilation error will be caused instead)。

语言要求当你从这样的类型派生时,你自己的析构函数必须有相同的限制:

virtual BadJumbleException::~BadJumbleException() throw() {}
// ^^^^^^^

这是因为覆盖函数可能没有宽松的抛出规范


在 C++11 中,std::exception::~exception 标记为 throw()(或 noexcept ) 在库代码中显式,但默认情况下所有析构函数都是 noexcept(true)

that rule would include your destructor and allow your program to compile ,这使我得出结论,您不是真的编译为 C++11。

关于c++ - C++ 中更宽松的抛出说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53050602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com