#include<iostream>usingnamespacestd;classString{public:/*String(constchar*str=""):_str(newchar[strlen(str)+1]){strcpy(_str,str);}*/String(constchar*str=""){if(str==NULL){_str=newchar;_str[0]='\0';}else{_str=newchar[strlen(str)+1];strcpy(_str,str);}}/*String(constString&other):_str(NULL){_str=newchar[strlen(other._str)+1];strcpy(_str,other._str);}*/String(constString&other):_str(NULL){Stringtmp(other._str);//创建一个临时对象swap(_str,tmp._str);}String&operator=(String&other){if(this!=&other){delete[]_str;_str=newchar[strlen(other._str)+1];strcpy(_str,other._str);}return*this;//返回值支持链式表达式}char*GetStr(){return_str;}char&operator[](size_tindex)//若不用引用,再它返回时会返回一个匿名对象为一个常量不可改变{return_str[index];}/*String&operator=(Stringother){swap(_str,other._str);//现代写法即传过来一个临时对象return*this;}*/~String(){if(_str)delete[]_str;}protected:char*_str;};voidTest(){char*ptr="abcd";Strings1(ptr);Strings2(s1);Strings3("def");s3=s2;//s1[0]='x';cout<<s3.GetStr()<<endl;}

若只定义一个全局的整型变量,它只适用于指向同一个内存块。

用引用计数解决浅拷贝后多次析构崩溃问题(写时拷贝),每块空间都有各自的指针指向。

#include<iostream>usingnamespacestd;

classString{public:String(constchar*str=""):_str(newchar[strlen(str)+5]){*((int*)_str)=1;//多开4字节空间保存有几个指针指向该块空间_str+=4;strcpy(_str,str);}/*String(constchar*str=""){if(str==NULL){_str=newchar;_str[0]='\0';}else{_str=newchar[strlen(str)+1];strcpy(_str,str);}}*/String(constString&other):_str(other._str){++*(((int*)_str)-1);}//String(constString&other)//:_str(NULL)//{//Stringtmp(other._str);//创建一个临时对象//swap(_str,tmp._str);//}String&operator=(String&other){if(this!=&other){if(--*(((int*)_str)-1)==0){//若只有一个指针指向该空间则释放掉它,让其重新指向另一块空间_str-=4;//释放空间时一定从头释放delete[]_str;}}_str=other._str;++*(((int*)(other._str))-1);return*this;//返回值支持链式表达式}char*GetStr(){return_str;}char&operator[](size_tindex)//若不用引用,再它返回时会返回一个匿名对象为一个常量不可改变{return_str[index];}/*String&operator=(Stringother){swap(_str,other._str);//现代写法即传过来一个临时对象return*this;}*/~String(){if(_str){if(--*(((int*)_str)-1)==0){_str-=4;//释放空间时一定从头释放delete[]_str;}}}protected:char*_str;};voidTest(){char*ptr="abcd";Strings1(ptr);Strings2(s1);Strings3("def");s3=s2;//s1[0]='x';cout<<s3.GetStr()<<endl;}