【问题标题】:What() method for std::exception isn't acting virtual?std::exception 的 What() 方法不是虚拟的?
【发布时间】:2015-08-04 23:52:41
【问题描述】:

所以在参考手册中,what() 方法被描述为虚拟的,但它似乎并没有那样做。 (我正在用 g++ 和 c++11 标志编译)

#include <stdio.h> //printf
#include <iostream> //cout
#include <stdexcept>// std::invalid_argument
using namespace std;

void fn(){
    throw runtime_error("wowwowo");
}

int main(){
    try {fn(); }
    catch(exception a) {cout << a.what() << endl;}
    return 0;
}

输出是“std::exception”,而不是错误消息“wowwowo”。但是,如果我将 catch 类型更改为 runtime_error,它会按预期运行。我有一些代码,我想捕获可能是也可能不是 runtime_errors 的异常,我想我可以有多个 catch 块,但我很好奇为什么代码的行为如此。这是打印出错误消息的代码:

#include <stdio.h> //printf
#include <iostream> //cout
#include <stdexcept>// std::invalid_argument
using namespace std;

void fn(){
    throw runtime_error("wowwowo");
}

int main(){
    try {fn(); }
    catch(runtime_error a) {cout << a.what() << endl;}
    return 0;
} 

【问题讨论】:

    标签: c++ exception c++11 virtual


    【解决方案1】:

    更改此语句:

    catch(exception a) {cout << a.what() << endl;}
    

    到这里:

    catch(const exception &a) {cout << a.what() << endl;}
    

    您必须通过引用捕获异常,以便它使用多态性。否则,您将分割 std::runtime_error 对象,因此只剩下一个 std::exception 对象,因此将调用 std::exception::what() 而不是 std::runtime_error::what()

    至于函数本身,确实是virtual函数。

    class exception {
    public:
        //...
        virtual const char* what() const noexcept;
    };
    

    【讨论】:

    • 未能通过引用捕获导致slicing
    • @Mark Ransom 一直以来,我都在尝试回忆英语单词 slicing.:)。谢谢。
    • 啊,谢谢你们。得跑了,我稍后会接受thios的回答。
    猜你喜欢
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2020-10-25
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    相关资源
    最近更新 更多