【发布时间】:2010-11-14 08:24:09
【问题描述】:
catch (exception e) { syslog (LOG_ERR, "exception: " + e.what()); }
这是我想要做的,但它不起作用,我试过用这个
string ctos(const char& c){
stringstream s;
s << c;
return s.str();
}
但还是输了
任何帮助将不胜感激。
【问题讨论】:
-
您是否尝试将
const char*(根据您的问题)或const char(通过引用传递,根据您的代码)转换为string?您的用法不清楚。 -
虽然它与问题无关,但我建议在
catch中使用const exception& e,否则您的异常对象将被截断为exception。 -
安全注意事项:作为一般规则,仅在 printf 或 syslog 类型格式字符串中使用字符串文字。如果是变量,则将其作为参数传递。在这个例子中,如果有人这样做:
throw std::runtime_error( std::string("Bad user input: ") + user_input )并且用户输入包含格式攻击怎么办?您的程序将是敬酒!
标签: c++ string pointers constants