Simple string class implementation and write-time copy

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.