标准库std::string写时拷贝
#include<string> #include<cstdio> int main() { std::string str1 = "Hello"; std::string str2 = str1; printf("str1:%x\n",str1.c_str()); printf("str2:%x\n",str2.c_str()); str1[1]='Q'; str2[1]='W'; printf("str1:%x\n",str1.c_str()); printf("str2:%x\n",str2.c_str()); return 0; }
运行结果:
str1:56133c str2:56133c str1:56135c str2:56133c
文章来自:陈皓博客:http://blog.csdn.net/haoel/article/details/24058