C++我不明白我的异常what()行为

use*_*614 2 c++ exception

我正在用what()方法编写一个异常类.

const char* what() const throw() {
    return "test";
}
Run Code Online (Sandbox Code Playgroud)

工作正常,但是

const char* what() const throw() {
    return (std::string("test")).c_str();
}
Run Code Online (Sandbox Code Playgroud)

似乎返回一个随机的结果.为什么?

Kon*_*lph 13

std::string("test")创建一个临时字符串对象.c_str返回指向该临时对象的某个内部存储的指针.函数退出后,这是一个悬空指针 - 它指向无效的内存.

没有办法绕过这个.您必须通过在函数const外部声明(并初始化,因为函数是)来使您的字符串对象更长寿- 或者您需要在函数内手动分配堆存储,在那里复制字符串,并返回指向存储.但是,这很复杂,容易出错,并且违反了函数的约定,因为错误类的用户不希望释放内存.

事实上,这是一个错误类的简单实现(玩具,因为我不知道你的用例),考虑到这一点:

struct my_logic_error : std::logic_error {
    // `std::logic_error` already overrides `what`, we only need to initialise it!
    my_logic_error(int errcode) :
        std::logic_error{std::string{"Logic error occurred with error code "} +
                std::to_string(errcode)} {}
};
Run Code Online (Sandbox Code Playgroud)

假设您从没有这种奢侈的异常类派生,您的代码变得最简单:

struct my_error : std::exception {
    my_error(int errcode) :
        message{std::string{"Logic error occurred with error code "} +
                std::to_string(errcode)} {}

    char const* what() const noexcept {
        return message.c_str();
    }

private:
    std::string message;
};
Run Code Online (Sandbox Code Playgroud)