<分区>
我进行了搜索,但没有找到我遇到的问题,但也许我只是不知道如何清楚地表达这个问题。
首先,我在相关平台上只有一个 C++98 编译器,所以如果您在回复时能记住这一点,我将不胜感激。
让我举个例子和我得到的错误信息:
namespace mal {
struct Exception : virtual std::runtime_error {
Exception(): std::runtime_error("mal exception") {}
explicit Exception(const char* msg): std::runtime_error(std::string(msg?msg:"")) {}
explicit Exception(const std::string& msg): std::runtime_error(msg) {}
virtual ~Exception() throw() {}
}; // Exception struct
struct OpenException : virtual Exception {
OpenException(): Exception("mal open error") {}
}; // OpenException struct
}; // mal namespace
我收到以下错误:
malt.cpp: In constructor ‘mal::OpenException::OpenException()’:
malt.cpp:18: error: no matching function for call to ‘std::runtime_error::runtime_error()’
OpenException继承Exception,Exception继承std::runtime_error并初始化为字符串。为什么 OpenException 初始化语句 Exception("mal open error")
不使用 Exception 的 const char* 重载来初始化 std::runtime_error?这不会通过继承层次向上传播吗?
如果 C 继承 B 而 B 继承 A,那么 C 必须知道 A 的实现细节似乎违反了 OO 原则。