#include <iostream>using namespace std;class string{public:/*string (const char* Str= " "): _str (New char[strlen (str) +1]) {strcpy (_STR,&NBSP;STR);} */string (const char* str = " ") {if (str == null) {_str = new char;_str[0] = ' + ';} Else{_str = new char[strlen (str) + 1];strcpy (_STR,&NBSP;STR);}} /*string (Const string& other): _str (NULL) {_str = new char[strlen (OTHER._STR) + 1];strcpy (_STR,&NBSP;OTHER._STR);} */string (Const string& other): _str (NULL) {string tmp (OTHER._STR);//create a temporary object swap (_str, TMP._STR);} String& operator= (String& other) {if (this != &other) {delete[] _str;_ Str = new char[strlen (OTHER._STR) + 1];strcpy (_STR,&NBSP;OTHER._STR);} return *this;//return value supports chain expression}char *getstr () {return _str;} Char& operator[] (size_t Index)//If no reference is returned, it returns an anonymous object as a constant immutable {Return _str[index];} /*string& operator= (string other) {swap (_STR,&NBSP;OTHER._STR);//modern notation passes over a temporary object return *this;} */~string () {if (_STR) delete[] _str;} protected:char *_str;}; Void test () {char* ptr = "ABCD"; STRING&NBSP;S1 (PTR); STRING&NBSP;S2 (S1); STRING&NBSP;S3 ("def");s3 = s2;//s1[0] = ' x '; cout<<s3. Getstr () << endl;}
If you define only a global integer variable, it only applies to pointing to the same block of memory.
Using reference counting to solve the problem of multiple destruction after shallow copy (copy on write), each space has its own pointer pointing.
#include <iostream>using namespace std;
Class string{public:string (const char* str = " "): _str (New char[strlen (str) &NBSP;+&NBSP;5] {* (int*) _str) = 1; //multiple open 4-byte space holds several pointers to the block space _str += 4;strcpy (_STR, &NBSP;STR);} /*string (const char* str = " ") {if (str == null) {_str = new char;_str[0] = ' + ';} Else{_str = new char[strlen (str) + 1];strcpy (_STR,&NBSP;STR);}} */string (Const string& other): _str (other._str) {++* (((int*) _str)-1);} String (Const string& other)//:_str (NULL)//{//string tmp (OTHER._STR);//Create a temporary object//swap (_str , tmp._str);//}string& operator= (String& other) {if (this != &other) { if (--* ((((int*) _str) - 1) {//If only one pointer is pointing to the space, release it and point it back to another space == 0 Always release delete[] _str from scratch when releasing space;}} _str = other._str;++* (((int*) (OTHER._STR))-1); return *this;//return value supports chained expressions}char&nbsP;*getstr () {return _str;} Char& operator[] (Size_t index)//If it is not referenced, then it returns an anonymous object for a constant immutable {Return _str[index];} /*string& operator= (string other) {swap (_STR,&NBSP;OTHER._STR);//modern notation passes over a temporary object return *this;} */~string () {if (_STR) {if (--* (((int*) _str) - 1) == 0 //release space when the DELETE[]&NBSP;_STR must be released from the beginning;}}} protected:char *_str;}; Void test () {char* ptr = "ABCD"; STRING&NBSP;S1 (PTR); STRING&NBSP;S2 (S1); STRING&NBSP;S3 ("def");s3 = s2;//s1[0] = ' x '; cout << s3. Getstr () << endl;}
This article is from the "Output Yang Hui triangle" blog, be sure to keep this source http://10541559.blog.51cto.com/10531559/1761837
Simple string class implementation and write-time copy