对此有何想法将不胜感激:
std::string s1 = "hello";
std::string s2 = std::string(s1);
我现在期望这两个字符串是独立的,也就是说,我可以将", world"添加到s2中,而s1仍然保持为"hello"。在Windows和Linux上,我发现了这一点,但是在HP_UX机器上运行代码时,它似乎s2和s1是同一个字符串,所以修改s2会改变s1。
听起来很疯狂,有人见过类似的情况吗?
对此有何想法将不胜感激:
std::string s1 = "hello";
std::string s2 = std::string(s1);
我现在期望这两个字符串是独立的,也就是说,我可以将", world"添加到s2中,而s1仍然保持为"hello"。在Windows和Linux上,我发现了这一点,但是在HP_UX机器上运行代码时,它似乎s2和s1是同一个字符串,所以修改s2会改变s1。
听起来很疯狂,有人见过类似的情况吗?
导致问题的代码如下:
#include <iostream>
#include <string>
class S : public std::string // An extension of std::string
{
public:
explicit S(const char* s)
: std::string(s)
{
}
};
class N // Wraps an int
{
public:
explicit N(int n)
: _n(n)
{}
operator S() const // Converts to a string extension
{
return _n == 0 ? S("zero") : (_n == 1 ? S("one") : S("other"));
}
private:
int _n;
};
int main(int, char**)
{
N n0 = N(0);
N n1 = N(1);
std::string zero = n0;
std::cout << "zero = " << zero << std::endl;
std::string one = n1;
std::cout << "zero = " << zero
<< ", one = " << one << std::endl;
return 0;
}
这一定是一个bug。std::string可能使用引用计数的字符串实现,但一旦它被更改,它应该“分叉”字符串。
这两个字符串实际上是否不同,或者您使用其他比较方式(例如c_str()返回的地址)?
一些string的实现在进行复制时并不会复制整个字符串,以加快长字符串的复制速度。如果您尝试对其中任何一个进行更改,则实现应该会复制该字符串并进行适当的更改(理想情况下作为同一操作的一部分)。
这肯定是一个缺陷。这个例子是从C++标准中直接摘录的:
string s1("abc");
string::iterator i = s1.begin();
string s2 = s1;
*i = ’a’; // Must modify only s1
这对我来说肯定是个bug。有其他人也能在HP/UX上重现这个问题吗?
你的意思是这个程序在两行上显示相同的文本?
#include <stdio.h>
#include <string>
int main ()
{
std::string s1 = "hello";
std::string s2 = std::string(s1); // note: std::string s2( s1); would reduce the number of copy ctor calls
s2.append( ", world");
printf( "%s\n", s1.c_str());
printf( "%s\n", s2.c_str());
}