sha*_*agz 1 c++ string variables constants tinyxml
我正在尝试使用 TinyXml 从属性值中获取 std::string。我唯一能得到的是 const char * val,我找不到任何方法将 const char * 转换为 std::string。
所以有两个可能的答案: 1. 如何使用 TinyXml 获取属性字符串?2.如何将const char * val转换为字符串val。
这是我现在的代码:
TiXmlElement* data;
data->Attribute("some_name"); // return const char * which seems like unconvertible.
Run Code Online (Sandbox Code Playgroud)
谷歌搜索后,我试过这个:
char * not_const= const_cast<char *> (data->Attribute("some_name"));
Run Code Online (Sandbox Code Playgroud)
代码本身没有错误,但是在编译和运行后我得到了异常。
std::string 有一个接受 char const* 的构造函数。你不需要 char* 。
std::string str = data->Attribute("some_name");
Run Code Online (Sandbox Code Playgroud)
但是,请注意 std::string 不喜欢 NULL 值,所以不要给它任何值。
| 归档时间: |
|
| 查看次数: |
4452 次 |
| 最近记录: |