Deep copy of String class (2 kinds) and shallow copy (3 kinds)

Source: Internet
Author: User
Tags shallow copy

#include <iostream>using namespace std;//deep copy://class string//{//public:////default construction//string ( Char *string= "")//:_str (New char[strlen (String) +1])//{//strcpy (_str, string);//}//// If the copy constructor or assignment constructor is not written, the system default copy is a shallow copy of the////initialization list, if it is not written, if it is a base type, it is initialized to a random value, and the custom type is tuned to the constructor//string (const string&  s)//:_str (New char[strlen (S._STR) +1])//{//strcpy (_STR,&NBSP;S._STR);//}////string&    operator =  (const string& s)//{//if (this!=&s)//       {//      delete[] _str;//will not be empty, no judgment, minimum string ""//  _str =  new char[strlen (S._STR)  + 1];//  strcpy (_STR,&NBSP;S._STR);// }//return  *this;//}//~string ()//{//delete[] _str;//}//void display ()//{//cout << _str  << endl;//}//private://char* _str;//};//class  string//{//// By calling the default constructor write-copy construction and assignment operator overloading//public://string (char* string =  "")//:_string (New char[strlen (String)  + 1])//{//strcpy (_string, string);// }//string (string& s)//:_string (NULL)//need to set pointer is empty, otherwise the pointer random value will be destroyed//{//string tmp (s._string);//// Using the TMP out-of-scope destructors, destroying the open memory, this function does not need//swap (_string, s._string);//must be two operands of the same type, so parameter parameters should not be const//}//string&  Operator= (string s)//Use Copy construction assignment//{//      //do not need to judge This==&s, here is copy construction S, address must be different Swap (_string, s._string);//return *this;////uses the S-destructor to destroy the *this of the _string pointer that was originally opened.//}////   String& operator= (const string& s)////  {////if  (this != & s)////{////string tmp (s);////swap (_string, tmp._string);////}////return *this;////}////}// Two-way assignment operator overloading//~string ()//{//if  (_string)//delete[] _string;//}//void display ()//{//cout  << _string << endl;//}//private://char* _string;//};//Shallow Copy//class string// {////static variable, this method restricts only the pointer to the copy construction object/public://string (char* string =  "")//:_string (New char[strlen (String)  + 1])//{// scount = 1;//strcpy (_string, string);//}//string (const string& s)//:_string (s._ String)//initialization list cannot be a static variable//{//scount = ++scount;//}//string& operator= (const string&  s)//{////can only be used with one of the same member pointers, without defining an assignment overload//}//~string ()//{//if  (--scount == 0)//{//delete[]  _string;//}////cout << scount << endl;//}//void display ()//{//cout  << _string << endl;////cout << scount << endl;//}// private://char* _string;//static int  scount;//};//int string::scount = 0;/ /class string//{////Each object defines a number of objects that store the same pointer//public://string (char* string= "")//:_string (New char [Strlen (String) +1]),//scount (New int (1))//{//strcpy (_string, string);//}//string (const string & s)//:_String (s._string),//scount (S.scount)//{//++ (*scount);//}//string& operator= (const string&  s)//{//if  (this != &s)//{//delete (*this);//_string = s._string;//scount  = s.scount;//++ (*scount);//}//return *this;//}//void delete (String& s)//{//if   (--(*s.scount)  == 0)//{//delete scount;//delete[] _string;//}//}//~string ()//{// Delete (*this);//}//void display ()//{//cout << _string << endl;//}// private://char* _string;//int *scount;//};//class string//{////gives the front more than 4 bytes to store an object that refers to the same member pointer//public ://string (char* str =  "")//:_str (New char[strlen (str)  + 5])//{//* ((int *) _ STR)  = 1;//_str = _str + 4;//strcpy (_STR,&NBSP;STR);//}//String (const  String& s)//:_str (S._STR)//{//++ (* (int *) (_str-4));//}//string& operator= (const  string& s)//{//if  (thiS != &s)//{//            delete (* this);//_str = s._str;//++ (* (int *) (_str-4));//}//return  *this;//}//~string ()//{// cout << * ((int*) (_str - 4))  << endl;//delete (*this);//      //}//void delete (String& s)//{//if  (--(* (int *) (_str-4)) == 0)// {//_str -= 4;//delete[]  _str;//}//}//void display ()//{//cout << _str  << endl;//cout << * (int*) (_str - 4)  << endl;////}// Private://char* _str;//};void test1 () {string s1 ("abcdef"); S1. Display (); STRING&NBSP;S2 (s1); S2. Display (); STRING&NBSP;S3 ("abc"); S3. Display (); S3 = s1;s3. Display ();} Int main () {Test1 (); System ("pause"); return 0;}


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1716558

Deep copy of String class (2 kinds) and shallow copy (3 kinds)

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.