c++ - 如何在 C++ 中使用自定义消息抛出 std::bad_alloc?

标签 c++ bad-alloc

自 VS2008 以来,类 bad_alloc 不提供带字符串参数的构造函数。是否有可能创建自定义消息而不覆盖这样的类?

// this is only pseudo-code
class custom_exception : bad_alloc {
public:
    string Message;
    custom_exception(string m) {Message = m;}
}

最佳答案

bad_alloc 需要在不分配内存的情况下构造,这就是为什么它的构造函数不采用消息参数的原因。您当然可以将其子类化并覆盖 what

关于c++ - 如何在 C++ 中使用自定义消息抛出 std::bad_alloc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14999496/

相关文章:

c++ - 没有歧义的标识符

c++ - 为什么 boost this_thread::interrupt 可以在没有 try-catch 的情况下工作?

c++ - 声明 new int[n] 时的 std::bad_alloc

c++ - 查找bad_alloc原因的调试策略

c++ - 使用内部删除功能

c++ - 何时使用堆而不是堆栈以及变量何时超出范围?

c++ - While 循环中的条件

c++ - 如何创建一个 void 数组,其大小将由用户从命令行输入指定?

c++ - 如何处理 C++ 中的 bad_alloc?

c++ - bad_alloc 在类 Texture 上调用 new 时