下面的代码工作得很好:
#include <exception>
using namespace std;
class FileException : public exception { // error occurs here
int _error;
// string _error; <-- this would cause the error
public:
FileException(int error);
// FileException(string error);
const char* what() const throw();
};
但是当我将_error
的类型更改为字符串时,就会出现以下编译错误:
Exception specification of overriding function is more lax than base version
最佳答案
std::string
的析构函数not不抛出,导致FileException
的隐式析构函数not 也不会抛出。但是 std::exception
的析构函数是 no-throw,因此存在编译错误。
您可以声明一个显式的不抛出析构函数:
virtual ~FileException() throw() {}
或者只是继承自 std::runtime_error
而不是 std::exception
,它有一个接受 std::string
输入的构造函数.
关于c++ - std::exception 子类,字符串成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618060/