char *buffer1 = "abc";
const char *buffer2 = (const char*) buffer;
std :: string str (buffer2);
这行得通,但我想声明 std::string 对象,即 str,一次 并多次使用它来存储不同的常量字符*。
出路在哪里?
最佳答案
您可以重新分配:
const char *buf1 = "abc";
const char *buf2 = "def";
std::string str(buf1);
str = buf2; // Calls str.operator=(const char *)
关于c++ - 如何在 std::string 中存储 const char*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6214160/