1. Why should we learn to write custom string classes
Interviewer Love test, you have a way, no-.-650) this.width=650; "src=" Http://img.baidu.com/hi/bobo/B_0004.gif "alt=" B_0004.gif "/>
2. How to write the custom string class correctly
Quote a word in C++primer:
class is not secure enough to be handled correctly, the designer of the class (i.e. we) is required to write copy Construction and assignment operator overloading functions, and the hardest thing is not how to write but let's themselves aware of the need to do so.
What I have to say about MyString is this: the issue of deep-and-dark copying, the reason is that its member variable is a char * type, if we are lazy to let the compiler help us to build constructs, copy constructs, the assignment operator overloads these functions, then the problem is very big drop, because it is also very lazy, What it does is let two pointers point to the same place.
For a chestnut look:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/8C/wKiom1cDpBuTLjo9AAHSuXrHeek863.jpg "title=" Image001.jpg "alt=" Wkiom1cdpbutljo9aahsuxrheek863.jpg "/>
So let's talk about how to write a string that's right:
First string in C + + It's a class, right?
Then we'll write a class out (member functions and member variables)
class mystring{private:char *_pdata;//Yes, you're right, you just need a char pointer to do it. public://first class to have a constructor-- Ensure that the member variables of the class are correctly initialized//The first one is----correct but not optimal mystring (char *pdata=null) {if (pdata==null) {_pdata=new char[1] ; _pdata[0]= ' + ';} Else{_pdata=new char[strlen (PData) +1];strcpy (_pdata,pdata);}} The second is better than the first: Use the initialization list mystring (char *pdata=null):p data (New char[strlen (PData) +1]) {strcpy (_pdata, PData);} Since the new in the construction is so relative, is it going to delete in the destructor? ~mystring () {if (_pdata)//here can be directly without judgment, think about why? {delete []_pdata;}} Copy construction-----? Why write, because the member variable is a pointer, if we do not go to write themselves//will appear security and correctness problems, two pointers to a space, when one of the destruction,///Another can not access the space, there will be illegal operation// Test Center: The parameter must pass in a reference to that type, otherwise, when the argument is passed to the formal parameter//, the value is passed and the copy is constructed, then the copy construct is a dead loop//The first notation MyString (const mystring &mstr ) {if (strlen (mstr._pdata) ==0) {_pdata=new char[1];_pdata[0]= '} ';} Else{_pdata=new char[strlen (Mstr._pdata) +1];strcpy (_pdata,mstr._pdata);}} Second notation MyString (CONST&NBSP;MYSTRING&NBSP;&MSTR): _pdata (New char[strlen (mstr._pdata) +1]) {strCPY (_pdata,mstr._pdata);} The third way: only in the construction and destruction of the time to open up and release space, memory space is not easy to error//Do not appear MyString instantiation of the object error, considering the exception security MyString (CONST&NBSP;MYSTRING&NBSP;&MSTR ): _pdata (NULL)//_pdata not initialized, random space, if not assigned to NULL, delete fails {mystring temp (mstr._pdata); swap (temp._pdata,_pdata);} Assignment operator overloading//test Center: 1, the return value is the type reference (considering the case a=b=c)//test Center: 2, the formal parameter is const reference (does not change the parameters and high efficiency)//The first one---> disadvantage: If the new char[ ] If there is an error, it is possible that the _pdata will become a wild pointer//Then the object returned by MyString is an incorrect object with an exception security problem mystring& operator= (const mystring &NBSP;&MSTR) {//Test center: 3, oneself assigns the value the situation, has not considered! if (THIS!=&MSTR) {///test Center: 4, first release, must be released []_pdata, the reason is the way of construction delete []_pdata;//and then open up _pdata=new char[ Strlen (Mstr._pdata) +1];strcpy (_pdata,mstr._pdata);} Return *this;} The second way mystring &operator= (CONST&NBSP;MYSTRING&NBSP;&MSTR) {//First open char *temp=new char[ Strlen (Mstr._pdata) +1];if (temp==null) {return *this;} In the release of delete []_pdata;_pdata=temp;strcpy (_pdata,mstr._pdata); return *this;} The Third Way mystring &operator= (MYSTRING&NBSP;MSTR) { &nbsP; swap (mstr._pdata,_pdata); return *this;} Better notation mystring &operator= (const mystring &mstr) { if (&mstr!= This) { mystring temp (mstr._pData); swap (Temp._pdata,_pdata); } return *this;} The string object is converted to CONST&NBSP;CHAR*CONST&NBSP;CHAR*&NBSP;C_STR () Const{return _pdata;} Find string length size_t size () {Return strlen (_pdata);} Determines whether equality bool operator== (CONST&NBSP;MYSTRING&NBSP;&MSTR) const{if (&mstr!=this) {if (!strcmp (_ Pdata,mstr._pdata)) {Return false;}} Return true;} A character char operator[] (Size_t pos) const{if (Pos<strlen (_pdata) &&pos>=0) {return _pdata[pos];} else{return 0;}} string comparison int operator< (CONST&NBSP;MYSTRING&NBSP;&MSTR) const{int truth=strcmp (_pData,mstr._pDATA);if (truth>0) {return -1;} else if (truth==0) {return 0;} else{return 1;}}};
This article from "Momo is spicy moe" blog, please be sure to keep this source http://momo462.blog.51cto.com/10138434/1760630
Why are those in C + + related to custom strings