The default functions in C + + deep copy and shallow copy deep assignment and shallow assignment value

Source: Internet
Author: User
Tags shallow copy

1, C + + Nothing is written, there are 6 default functions, automatically provided by the system:

constructor, copy constructor, assignment statement, destructor, fetch address character of general object, take address overload of constant object;

Overloads for the & operator:

Test T3;

Test *pt = &t3;

test* operator& () {return this;}

Take address overloading on a constant object;

const Test T4;

Const Test *PT1 = &t4;

Const test* operator& () const{return this;

2, deep copy and shallow copy

#include <iostream> #include <string.h> #include <malloc.h>using namespace std;class  string{public:    string (const char *str =  "") {         if (str == null) {             data = new char;             data[0] = 0;        }else{             data = new char[strlen (str)  +  1];            strcpy (DATA,&NBSP;STR);         }    }    ~string () {         delete []data;    }private:     char *data;};Int main (void) {    string t1 ("abcdef");     string t2 &NBSP;=&NBSP;T1;&NBSP;&NBSP;&NBSP;&NBSP;STRING&NBSP;T3 ("Hello");    t3 = t1;     return 0;}

The operating structure is as follows:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/84/75/wKiom1eQ3WfRI_apAABkaFr5YyI564.png-wh_500x0-wm_3 -wmp_4-s_2089143032.png "title=" Qq20160721223357.png "alt=" Wkiom1eq3wfri_apaabkafr5yyi564.png-wh_50 "/>

You know, the program is broken, we use the system default copy constructor and assignment statement, this is only the assignment between the members,

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/84/75/wKioL1eQ3r2y70aqAAAV7h2_CEY347.png-wh_500x0-wm_3 -wmp_4-s_172457856.png "title=" Qq20160721223933.png "alt=" Wkiol1eq3r2y70aqaaav7h2_cey347.png-wh_50 "/>

Two objects through the default copy construction, the member data is assigned to each other, so that data is different, the value of data is the same, pointing to the same space;

At this point in the last call to the destructor, formed a multiple release of the same space, is the wrong operation!!! , this is a shallow copy.

Two objects through the default assignment statements, the member data is assigned to each other, making data different, the value of data is the same, pointing to the same space, shallow assignment.

solution, you should write your own copy construction and assignment statements to achieve a deep copy:

String (const string &s) {data = new Char[strlen (s.data) + 1];    strcpy (data, s.data);            } string& operator= (const String &s) {if (This! = &s) {delete []data;            data = new Char[strlen (s.data) + 1];        strcpy (data, s.data);    } return *this; }

Deep copy and deep assignment means: Re-apply for space, each save their own, finally in their own release, to ensure the security of memory access;

Deep assignment should note the following four steps:

(1), judge whether you assign value to yourself

(2) , release the original space//may be in the construction of the object, has pointed to a space, this time must be released, or memory leaks

(3), application space to assign value

(4), return to reference space


The default functions in C + + deep copy and shallow copy deep assignment and shallow assignment value

Related Article

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.