三个不同的编译器显示编译此代码的三种不同行为:
class MyException : public std::exception
{
public:
MyException(std::string str) : m_str(str) {}
virtual const char * what() const throw () {return m_str.c_str(); }
protected:
std::string m_str;
};
Run Code Online (Sandbox Code Playgroud)
Sun C++ 5.8 Patch 121017-22 2010/09/29:警告函数MyException :: 〜MyException ()只能抛出函数std :: exception :: ~exception()抛出的异常它会覆盖
g ++ 3.4.3: `virtual MyException:~MyException()'的错误抛出说明符
Visual Studio 2005:非常高兴(既不是错误也不是警告)
class exception {
public:
exception () throw();
exception (const exception&) throw();
exception& operator= (const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw(); …
Run Code Online (Sandbox Code Playgroud) 以下代码生成"Looser throw specifier error".你能帮我解决这个错误吗?
class base
{
virtual void abc() throw (exp1);
}
void base::abc() throw (exp1)
{
......
}
class sub : public base
{
void abc() throw(exp1, exp2);
}
void sub::abc() throw (exp1, exp2)
{
.....
}
Run Code Online (Sandbox Code Playgroud)