【问题标题】:Why does std::ios_base::failure have two inheritance diagrams and what is the difference?为什么std::ios_base::failure有两个继承图,有什么区别?
【发布时间】:2012-07-30 14:51:12
【问题描述】:

根据cppreference.comstd::ios_base::failure有两个继承层次:

问题

为什么和有什么区别?

背景

使用 g++ 4.7.1 我遇到了这个错误,到目前为止我知道这是因为第一个继承层次结构:

错误:“const class std::ios_base::failure”没有名为“code”的成员

更新

使用 gcc,即使在 C++11 模式下,ios_base::failure 仍然继承自 exception。引用here

ios_base::failure 不是从 system_error 派生的。

【问题讨论】:

    标签: c++ exception


    【解决方案1】:

    那是因为std::system_error 是在 C++11 中引入的。 std::ios_base::failure 之前直接派生自 std::exception

    由于code() 方法是std::system_error 的成员,std::ios_base::failure 不会在不支持 C++11 的环境中公开它。

    【讨论】:

    • 你可以在C++11标准的27.5.3.1.1找到它
    • 啊,有道理,我有点惊讶 g++ 4.7.1 还没有实现它。
    • @JesseGood:你用-std=c++11编译?
    • @Xeo:是的,我也有,我也有peeked in the header,据我所知,它没有实现(除非我遗漏了什么)。
    • 现在好像在libstdc++4.8.1实现了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2015-03-11
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多