C + + Learning Note Constructor (iii) copy (copy) constructor

Source: Internet
Author: User
Tags shallow copy

Definition: Use one object of a class to construct and initialize another object, which does not exist before the other object.

It is important to understand the difference between copy (copy) and assignment, when copying and copying another object does not exist, and the assignment is two objects are constructed.

such as: A;  A B (a);   A b = A; This is a copy.

A;  A b; b = A; This is a value assignment.

The overload declaration for the copy constructor is as follows:

A (const a& Other)

Example:

Class A

{

Private

int m_i;

Public

A ()

{

}

A (const a&other): M_i (other.m_i)

{

}

};

In C + +, the following three scenarios in which an object needs to call a copy constructor:

1) An object is passed into the function body in the way of value transfer;

2) An object is returned from the function in the way that the value is passed;

3) An object needs to be initialized with another object.

If the copy constructor is not used in the first two cases, it causes a pointer to point to the memory space that has been deleted (this sentence has not been fully understood, to be explained).

Shallow copy and deep copy

If a class has resources (such as pointers), when the object of this class is copied, if the resource is not redistributed, it is a shallow copy; If the resource is reassigned, it is a deep copy. What do you mean, for example:

Class Array

{

Private

int m_icount;

int *m_parr;

Public

Array ()

{

M_icount = 10;

M_parr = new Int[m_icount];

}

Array (const array& Other)

{

M_icount = Other.m_icount;

M_parr = Other.m_parr;

}

};

int main ()

{

Array arr1;

Array arr2 = arr1; Call copy constructor

return 0;

}

The code above is a shallow copy, but what is the problem, the copy constructor of array does not reallocate memory to the Copy object, Arr1.m_parr and Arr2.m_parr point to the same piece of memory, when ARR1 is destroyed, arr1.m_ Parr points to the memory is released, then Arr2.m_parr point to the memory does not exist, the corresponding Arr2.m_parr becomes a wild pointer. In the actual programming design to avoid shallow copy, the use of deep copy. What is a deep copy, changing the copy constructor of the above code is as follows:

Array (const array& Other)

{

M_icount = Other.m_icount;

M_parr = new Int[m_icount];

for (int i=0;i<m_icount;i++)

{

M_parr[i] = Other.m_parr[i];

}

}

  

C + + Learning Note Constructor (iii) copy (copy) constructor

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.