std::exception 的 What() 方法不是虚拟的?

What() method for std::exception isn't acting virtual?

所以在参考手册中,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" 相对。但是,如果我将捕获类型更改为 runtime_error,它会按预期运行。我有一些代码,我想在其中捕获可能是 runtime_errors 也可能不是 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;
} 

更改此语句:

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;
};