#includeusing namespace std;class String{public: /*String(const char* str=" ") :_str(new char[strlen(str)+1]) { strcpy(_str, str); }*/ String(const char* str = " ") { if (str == NULL) { _str = new char; _str[0] = '\0'; } else { _str = new char[strlen(str) + 1]; strcpy(_str, str); } } /*String(const String& other) :_str(NULL) { _str = new char[strlen(other._str) + 1]; strcpy(_str, other._str); }*/ String(const String& other) :_str(NULL) { String tmp(other._str);//创建一个临时对象 swap(_str, tmp._str); } String& operator=(String& other) { if (this != &other) { delete[] _str; _str = new char[strlen(other._str) + 1]; strcpy(_str, other._str); } return *this;//返回值支持链式表达式 } char *GetStr() { return _str; } char& operator[](size_t index)//若不用引用,再它返回时会返回一个匿名对象为一个常量不可改变 { return _str[index]; } /*String& operator=(String other) { swap(_str, other._str);//现代写法即传过来一个临时对象 return *this; }*/ ~String() { if (_str) delete[] _str; }protected: char *_str;};void Test(){ char* ptr = "abcd"; String s1(ptr); String s2(s1); String s3("def"); s3 = s2; //s1[0] = 'x'; cout< << endl;}
若只定义一个全局的整型变量,它只适用于指向同一个内存块。
用引用计数解决浅拷贝后多次析构崩溃问题(写时拷贝),每块空间都有各自的指针指向。
#includeusing namespace std;
class String{public:String(const char* str = " "):_str(new char[strlen(str) + 5]){*((int*)_str) = 1; //多开4字节空间保存有几个指针指向该块空间_str += 4;strcpy(_str, str);}/*String(const char* str = " "){if (str == NULL){_str = new char;_str[0] = '\0';}else{_str = new char[strlen(str) + 1];strcpy(_str, str);}}*/String(const String& other):_str(other._str){++*(((int*)_str)-1);}//String(const String& other)//:_str(NULL)//{//String tmp(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_t index)//若不用引用,再它返回时会返回一个匿名对象为一个常量不可改变{return _str[index];}/*String& operator=(String other){swap(_str, other._str);//现代写法即传过来一个临时对象return *this;}*/~String(){if (_str){if (--*(((int*)_str) - 1) == 0){_str -= 4; //释放空间时一定从头释放delete[] _str;}}}protected:char *_str;};void Test(){char* ptr = "abcd";String s1(ptr);String s2(s1);String s3("def");s3 = s2;//s1[0] = 'x';cout << s3.GetStr() << endl;}