【发布时间】:2013-04-05 22:12:54
【问题描述】:
下面的代码有问题。
#include <iostream>
#include <stdexcept>
class MyException : public std::logic_error {
};
void myFunction1() throw (MyException) {
throw MyException("fatal error");
};
void myFunction2() throw (std::logic_error) {
throw std::logic_error("fatal error");
};
int main() {
try {
myFunction1();
//myFunction2();
}catch (std::exception &e) {
std::cout << "Error.\n"
<< "Type: " << typeid(e).name() << "\n"
<< "Message: " << e.what() << std::endl;
}
return 0;
}
throw MyException("fatal error"); 行不起作用。 Microsoft Visual Studio 2012 表示:
error C2440: '<function-style-cast>' : cannot convert from 'const char [12]' to 'MyException'
MinGW 的反应非常相似。
这意味着构造函数std::logic_error(const string &what) 没有从父类复制到子类中。为什么?
感谢您的回答。
【问题讨论】:
标签: c++ oop exception inheritance constructor