C + + (c + + copy builder, assignment operator overloading)

Source: Internet
Author: User

Copy Builder

Creates a new object from an object that already exists. This means that the new object is not constructed by the constructor, but is done by the Copy builder. The format of the copy constructor is fixed.

class 类名{    类名(const 类名 & another)    拷贝构造体}class A{    A(const A & another)    {}}

Rules:

The 1 system provides a default copy constructor. Once realized, no longer exists.
2 The system provides a copy of the time, such as the so-called shallow copy.
3 to implement a deep copy, you must customize it.
A 4-shallow copy can cause a memory re-destructor. A shallow copy of Linux hangs on the machine. Double free, in some cases (with heap space), to implement self-copy constructs

#include <iostream>#include "mystring.h"using namespaceStdintMain () {string s ="Assassin";    String SS (s); cout<<"++++++++++++++++"<<endl;    cout<<ss<<endl; cout<<"++++++++++++++++"<<endl; mystring S1 ="INTELWISD"; MyString SS1 (S1);//Shallow copy, two objects point to the same address space, and when the object is disposed, the address space that the object points to is freed two times. cout<<"++++++++++++++++"<<endl;    Cout<<ss1.c_str () <<endl; cout<<"++++++++++++++++"<<endl;    String SSS = s; MyString sss1 = S1;//can also be implemented, the essence is also a copy, with an existing object to complete an object, from scratch the creation process. String ssss;    SSSs = s;    MyString Ssss1; Ssss1 = S1;//default can also, essentially assignment operator overload---> this pointer.     return 0;}
#ifndef MYSTRING_H#define MYSTRING_Hclass mystring{public:    //mystring();    mystring(constchar *s = NULL);//无参的形式包含在里面    char * c_str();    mystring(const mystring & another);    ~mystring();private:    char * _str;};#endif // MYSTRING_H
#include <iostream>#include "mystring.h"#include "string.h"using namespaceStd;mystring::mystring (Const Char*s) {if(s = = NULL) {_str =New Char[1]; *_str =' /'; }Else{intLen = strlen (s); _str =New Char[Len+1];    strcpy (_str,s); }}Char* MYSTRING::C_STR () {return_str;} Mystring::mystring (ConstMyString & Another) {//_str = another._str;//There is no privacy between the peers, so shallow replication can cause memory refactoring.     intLen = strlen (ANOTHER._STR); _str =New Char[Len+1]; strcpy (_STR,ANOTHER._STR);} Mystring::~mystring () {Delete[]_str;}
This pointer

A pointer to the current object that is generated by default when the system creates an object. The purpose of this is to bring convenience.

Role

1, avoid the same member name as the constructor's entry.
2, self references based on this pointer are also widely used in functions that support multiple concatenation calls.

class  stu{public : Stu (String name,< span class= "DT" >int age) {this ->name = name; this ->age = age; } void  display () {Cout<<name<< <<AGE&L t;<endl; } Stu & Growup () {this ->age++; return  *this ; } private : string name; int  age;} int  Main () {Stu s ( "Assassin" , 23 ); Dout<< "&s:"  <<&s<<endl; S.display (); S.growup (). Growup (). Growup (). Growup (). display (); //age increase  return  0 ;} 
Assignment operator overloading (operator=)

Assign a value to another object with one of its own objects. The assignment behavior that occurs after two objects have been created.
Format:

类名{    operator=(const 类名& 源对象)    拷贝体}class A{    operator=(const A& another)    {    //函数体        return *this;    }};

Rules:

The 1 system provides the default assignment operator overload, which, once implemented, ceases to exist.
2 The system provides a copy of the same bit, also shallow copy, will cause memory leaks, re-destruction.
3 to implement a deep assignment, you must customize it.
4 customizations are faced with three issues:

1, self-assigned value
2, Memory leak
3, re-destructor.
5 Returns a reference and cannot be decorated with a const. A = b = c = = (a+b) = C

mystring & mystring::operator=(const mystring & another){    if(this == &another)//复制自己的情况        return *this;    delete []this->_str;//先把自己的释放掉    int len = strlen(another._str);    thisnewchar [len+1];    strcpy(this->_str,another._str);    return *this;}

Full code:

#include <iostream>#include "mystring.h"#include "string.h"using namespaceStd;mystring::mystring (Const Char*s) {if(s = = NULL) {_str =New Char[1]; *_str =' /'; }Else{intLen = strlen (s); _str =New Char[Len+1];    strcpy (_str,s); }}Char* MYSTRING::C_STR () {return_str;} Mystring::mystring (ConstMyString & Another) {//_str = another._str;//There is no privacy between the peers, so shallow replication can cause memory refactoring.     intLen = strlen (ANOTHER._STR); _str =New Char[Len+1]; strcpy (_STR,ANOTHER._STR);} Mystring::~mystring () {Delete[]_str;} mystring& mystring::operator=(ConstMyString & Another) {if( This= = &another)//Copy your own case        return* This;Delete[] This->_str;//Release your own first    intLen = strlen (ANOTHER._STR); This->_str =New Char[Len+1]; strcpy This-&GT;_STR,ANOTHER._STR);return* This;}
 #ifndef mystring_h   #define MYSTRING_H     class  mystring{public : //mystring ();  MyString (const  char  *s = NULL);    //is contained in the form of a non-parametric     char  * C_STR ();    MyString (const  mystring & another);    mystring& operator  = (const  mystring & another); ~mystring (); private : char  * _STR;};  #endif  //mystring_h   
 #include <iostream>   #include "mystring.h"  using  namespace  Std;int  Main () {string s =  "assassin" ;    String SS (s);    Cout<< "++++++++++++++++"  <<endl;    cout<<ss<<endl;    Cout<< "++++++++++++++++"  <<endl;    mystring S1 =  "INTELWISD" ; MyString SS1 (S1); //shallow copy, two objects point to the same address space, freeing the object when the address space that the object points to is freed two times.     Cout<< "++++++++++++++++"  <<endl;    Cout<<ss1.c_str () <<endl;    Cout<< "++++++++++++++++"  <<endl;    String sss;    SSS = s; MyString Sss1;    // sss1 = S1; return  0 ;}  

C + + (C + + copy constructor, assignment operator overloading)

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.