为什么孙 class 的 parent 不处理孙 parent class 的初始化?

Why doesn't a grandchild class's parent handle the initialization of the grandparent class?

我进行了搜索,但没有找到我遇到的问题,但也许我只是不知道如何清楚地表达这个问题。

首先,我在所讨论的平台上只有一个 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的实现细节,这似乎违背了面向对象的原则。

如果您有一个 virtual 仅从基础 class 继承的 class,那么最派生的 class 总是直接负责为虚拟基地 class。这是为了防止钻石继承中的问题,其中一个 class 几乎继承自两个 classes,每个 classes 实际上继承自一个共享基础。在那种情况下,两个超级class中的哪一个应该初始化基class?

请注意,在常规继承中,不会发生这种情况。