【发布时间】:2018-05-02 16:24:20
【问题描述】:
我正在尝试实现自定义类异常。
异常本身有效,但我收到损坏的输出
#include <stdexcept>
namespace Exception{
class LibraryException : public std::runtime_error
{
public:
explicit LibraryException(const std::string& message)
: std::runtime_error(""),
prefix_("LibraryException: "),
message_(message)
{
}
const char* what() const noexcept override
{
std::string out;
out += prefix_;
out += message_;
return out.c_str();
}
private:
std::string prefix_;
std::string message_;
};
class BadSizeException : public LibraryException
{
public:
explicit BadSizeException() : LibraryException("Library: Bad Size\n")
{
}
};
}
当我尝试引发异常时的输出:
°áó°áóxception:尺寸错误
我做错了什么?
【问题讨论】:
-
一旦
what函数返回(使用指针)局部变量out(以及返回的指针指向的数据)会发生什么?
标签: c++ string c++11 exception