我正在开发一个使用自制报告库的软件,它只接受 (const char *) 类型作为参数:
ReportInfo(const char* information);
ReportError(const char* error);
[...]
当我尝试报告整数的值时,我想以 const char*
类型获得这些整数的表示。
我可以那样做:
int value = 42;
string temp_value = to_string(value);
const char *final_value= temp_value.c_str();
ReportInfo(final_value);
如果我能在不实例化 temp_value 的情况下做到这一点,那就太好了。 我试过这个:
int value = 42;
const char* final_value = to_string(value).c_str();
ReportInfo(final_value);
但在这种情况下 final_value
等于 '\0'。
知道怎么做吗?
最佳答案
你可以试试
ReportInfo(to_string(value).c_str());
因为在 ReportInfo
返回之前临时文件不会被销毁。
关于c++ - 在一行中获取整数的 (const char*) 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31828104/