c++ - Tinyxml 打印属性

标签 c++ variables string constants tinyxml

我正在尝试使用 TinyXml 从属性值中获取 std::string 。 我唯一能得到的是 const char * val,而且我找不到任何方法从 const char * 转换为 std::string。

有两个可能的答案: 1. 如何用TinyXml获取属性的字符串? 2.如何将const char * val转换为string val。

这是我现在的代码:

TiXmlElement* data;
data->Attribute("some_name"); // return const char * which seems like unconvertible.

谷歌搜索后,我尝试了这个:

char * not_const= const_cast<char *> (data->Attribute("some_name"));

代码本身没有错误,但是编译运行后出现异常。

最佳答案

std::string 有一个采用 char const* 的构造函数。您不需要为此使用 char*。

std::string str = data->Attribute("some_name");

但是,请注意 std::string 不喜欢 NULL 值,因此不要给它任何值。

关于c++ - Tinyxml 打印属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2369387/

相关文章:

Python:将复杂的字符串解析成可用的数据进行分析

C++构造函数隐式转换没有发生

c++ - fopen : is it good idea to leave open, 还是使用缓冲区?

c++ - 使用指针引用

python - 在 C 和 python 之间通信变量和字符串

wpf - 如何在 slider 值更改时使 TextBlock.Text = Slider.Value

java - 如何用Java中的空格替换字符串中的奇数/偶数字符?

C++ 无序_Map : Receiving "error: expected unqualified-id before ‘[’ token"at Compile

java - 是否可以通过引用将变量传递给函数?

swift - 如何将存储在变量中的字符串快速转换为可执行代码文本?