Understanding of deep copies and shallow copies

Source: Internet
Author: User

1. The two copies are used primarily for copying between objects!

2. Differences

No pointers:

Deep copy and shallow copy are no different;

With pointers:

Shallow copy: That is, the default copy function of the object, just copy the address of the pointer to the object, two variables point to an address at the same time, so when the destruction of the process will inevitably lead to the program crash;

1 classA2 {3  Public:4 A ()5     {6M_data =New Char[ -];7     }8~A ()9     {Ten         Delete[]m_data; OneM_data =NULL; A     } -  - protected: the Private: -     Char*m_data; - }; -  + int_tmain (intARGC, _tchar*argv[]) - { + A; A a B (a); at  -     return 0; - 

Deep copy: That is, to customize the copy function, save the pointer new section of memory, so that the destruction will not collapse;

  

Understanding of deep copies and shallow copies

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.