A common interview question for C + + is to get you to implement a String class, where I write the various notation of the string class
1. Shallow copy
#define _crt_secure_no_warnings 1#include<iostream>using namespace std;//1. Shallow copy class string{public: string (CHAR*&NBSP;STR) :_str (str) {} string ( Const string& s) :_str (S._STR) {} //~string () //{ // delete[] _str; //} cannot be freed two times, whether it is a constructor or a copy construct that simply points the pointer to the same space private: char* _str;}; Void test1 () { string s1 ("S1"); string s2 (S1);} Int main () { test1 (); system ("Pause" ); return 0;}
For shallow copies, this is not a mistake if we don't write destructors, but once the destructor is written, it can be problematic, as the writing of the Test1, like a test function, will release two times the space S1 points to, the program will crash, and if you want to improve it, you need a more robust approach.
2. Deep copy--traditional notation
The advantage of this writing is that it re-opens up space in both construction and copy construction, no matter how many times the program will not collapse, and can be improved in terms of operability
Class string{public: /*string () :_ STR (new char[1]) { _str[0] = ' }*/ string ' (char* str = "") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;:_STR (New char[strlen (str) + 1]) { strcpy (_STR,&NBSP;STR); } string (const string& s) :_str ( New char[strlen (S._STR) + 1]) { strcpy (_STR,&NBSP;S._STR); } string& Operator= (const string&s) //overloaded 1, imperfect { if (this != &s) { delete[] _str; _str = new char[strlen (S._STR) + 1]; strcpy (_STR,&NBSP;S._STR); } return *this; } string& operator= (Const string& s)//overloaded 2, more sound ideas { char* tmp = new char[strlen (S._STR) + 1]; strcpy (TMP,&NBSP;S._STR); delete[] _str; _str = tmp; } &nbSp; ~string () { /*if (_STR) */ delete[] _str; } private: char* _str;}; Void test1 () { string s1 ("S1"); //string s3 (); &NBSP;&NBSP;&NBSP;&NBSP;STRING&NBSP;S2 (s1);} Int main () { test1 (); system ("Pause"); return 0;}
The traditional way of writing is based on the way most people go, so that the program is more readable, the wording is mainly two kinds of overloaded, the first one does not consider whether new can open up enough space, directly released the original _STR, the second way is to first apply for space and then release, This way of thinking is more rigorous.
3. Deep copy--modern notation
Modern writing is based on the traditional wording of the improvement, the code is more concise and efficient, but logic has become more complex.
Class string{public: string (char* str = "") &NBSP;&NBSP;&NBSP;&NBSP;:_STR (New char[strlen (str) + 1]) { strcpy (_STR,&NBSP;STR); } String (const string& s) :_str (NULL) { string tmp (S._STR); swap (_STR,&NBSP;TMP._STR); } string& operator= (String s)//For the existing two string type so the direct exchange can the hidden test to generate a temporary space, after the exchange will be able to release the original space { swap (_STR,&NBSP;S._STR); return *this; } ~string () { /*if (_STR) */ delete[] _str; }private: char* _str;}; Void test1 () { string s1 ("S1"); //string s3 (); &NBSP;&NBSP;&NBSP;&NBSP;STRING&NBSP;S2 (s1);} Int main () { test1 (); system ("Pause"); return 0;}
Summarize the traditional writing and modern wording:
Traditional notation: 1. High readability, in line with the normal thinking of people
Modern notation: 1. Simple 2.new/delete are in a unified place, high maintainability
"C Language" "Face question" C + +, a shallow copy of string class, the traditional writing and modern writing of deep copy