eep*_*epp 1 c++ runtime-error exception std
我有一个继承自std::runtime_error这样的类:
#include <string>
#include <stdexcept>
class SomeEx : public std::runtime_error
{
public:
SomeEx(const std::string& msg) : runtime_error(msg) { }
};
Run Code Online (Sandbox Code Playgroud)
表示msg总是类似"无效的类型ID 43".有没有办法用另一个构造函数(或另一个方法)构建"什么字符串",以便我只提供整数类型ID?就像是:
SomeEx(unsigned int id) {
// set what string to ("invalid type ID " + id)
}
Run Code Online (Sandbox Code Playgroud)
static std::string get_message(unsigned int id) {
std::stringstream ss;
ss << "invalid type ID " << id;
return ss.str();
}
SomeEx(unsigned int id)
: runtime_error(get_message(id))
{}
Run Code Online (Sandbox Code Playgroud)
无关:我们有字符串的原因.what()是人们停止使用错误号码.
| 归档时间: |
|
| 查看次数: |
1172 次 |
| 最近记录: |