简体   繁体   English

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

[英]How to throw std::bad_alloc with a custom message in C++?

Since VS2008 the class bad_alloc does not provide a constructor with string parameter. 从VS2008开始,类bad_alloc不为构造函数提供字符串参数。 Is there any possibility to create a custom message without overwriting the class like this? 是否有可能在不覆盖此类的情况下创建自定义消息?

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

bad_alloc needs to be constructible without allocating memory, which is why its constructor does not take a message parameter. bad_alloc需要在不分配内存的情况下是可构造的,这就是为什么其构造函数不使用message参数的原因。 You can of course subclass it and override what . 您当然可以将其子类化并覆盖what

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM