Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
std::string s = t;
我认为在此之后, s 和 t 指向同一个地址。
一旦t被删除,s也是空的。如何制作深拷贝?
t
s
免责声明:我假设t也是一个std::string,如果我错了,请纠正我。
std::string
我认为在此之后, s 和 t 指向地址。
他们没有,他们不是指针。
一旦 t 被删除,s 也为空
t不能删除,或者如果你删除它,那是你的问题。不要删除t,它会自动超出范围。
在您的代码中,您使用std::string的复制构造函数 - 顾名思义,它创建原始字符串的副本。
这是不正确的,它们是字符串的完全独立的副本。当一个被删除时,另一个仍然存在。赋值运算符正是您应该复制这些的方式。