c ++将一个std :: string转换为另一个

use*_*186 -6 c++ string pointers

正如你可能从我的问题中得知的那样,我是一个c ++新手

我不确定将一个字符串的值复制到另一个字符串的正确方法是什么.

比如我这样做了:

std::string x = "this is x";
std::string y = "this is y";
x = y;
Run Code Online (Sandbox Code Playgroud)

没有错误,但这没有做任何事情..

我知道有字符串:: copy ...但似乎这个函数需要缓冲区大小等...这很烦人..

有没有一种简单的方法在c ++中完成这项工作?

谢谢

UPDATE!

对不起,我觉得我的答案太简单了.....这是我的情况:

class MyClass {
    std::string m_str = "OLD STRING";
}

void CopyString(MyClass& c, std::string x) {
    c.m_str = x;
}

int main() {
    MyClass c;
    CopyString(c, "NEW STRING");

    std::cout << c.m_str << std::endl;           // prints "OLD STRING"!!
}
Run Code Online (Sandbox Code Playgroud)

Che*_*Alf 5

你的代码

std::string x = "this is x";
std::string y = "this is y";
x = y;
Run Code Online (Sandbox Code Playgroud)

是正确的,并将y变量的值复制到x变量.

你的断言

"这没有做任何事情.

是 - 幸福 - 不正确.