C ++ learning-copy constructor

Source: Internet
Author: User

C ++ learning-copy constructor

I. Declaration of copying Constructors

Array (const Array & arr );

2. The implementation of copy constructor is divided into two types: Deep copy and shallow copy.

1. Shallow copy

The Code is as follows:

Class Array

{

Public:

Array ()

{

M_iCount = 5;

M_pArr = new int [m_iCount];

}

Array (const Array & arr)

{

M_iCount = arr. m_iCount;

M_pArr = arr. m_pArr;

}

Private:

Int m_iCount;

Int * m_pArr;

}

Int main (void)

{

Array arr1;

Array arr2 = arr1;

Return 0;

}

This simple shallow copy will make the m_pArr Member values in arr1 and arr2 the same, that is, they point to the same memory address. As shown in. At this point, if some values are assigned to m_pArr of arr1, that is, some values are written in the memory address 0x00FF00, and then assigned to m_pArr of arr2, the same memory segment will be overwritten, and the value assigned by m_pArr of arr1 will be overwritten. What's more serious is that when we destroy the arr1 object, we will release the memory point of m_pArr to avoid Memory leakage. If we have released this part of memory and then destroy the arr2 object, we will certainly release the part of memory directed by m_pArr in arr2 in the same way, in this way, the same memory segment will be released twice, and the computer will protest in the form of a crash, which is different from the syntax error prompt information and is difficult to find the root cause of the error.

2. Deep copy

Therefore, we want to copy the constructor to do this, as shown in. The pointers of the two objects should point to different memory. During copying, the pointer address should not be simply copied, instead, we copy each element stored in the address pointed to by the pointer sequentially. This is what we really want. This process is a deep copy.

The Code is as follows:

Class Array

{

Public:

Array ()

{

M_iCount = 5;

M_pArr = new int [m_iCount];

}

Array (const Array & arr)

{

M_iCount = arr. m_iCount;

M_pArr = new int [m_iCount];

For (int I = 0; I

{

M_pArr [I] = arr. m_pArr [I];

}

 

 

// M_pArr = arr. m_pArr;

}

Private:

Int m_iCount;

Int * m_pArr;

}

Int main (void)

{

Array arr1;

Array arr2 = arr1;

Return 0;

}

 

3. Summary:

When copying objects, it is not simply worth copying, but also copying the memory data in the heap. This copy method is called Deep copy, the method for copying only is called shortest copy. Deep copy has the following features:

(1) When a data member contains a pointer, the shortest copy will direct the pointer of the two objects to the same address. In this case, the address is deleted repeatedly or assigned a value again.

(2) Not all objects need to be copied in depth and should be used as appropriate.

(3) Deep copy is not a simple copy of values, but instead a memory address of the same size is re-allocated to copy the memory data pointed to by pointer members.

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.