Understanding of deep copy and light copy

Source: Internet
Author: User

Understanding of deep copy and light copy

1. The two copies are mainly used for copying objects!

2. Differences

No pointer:

There is no difference between deep copy and light copy;

Pointer:

Shallow copy: the default copy function of the object. It only copies the pointer address to the object, and the two variables point to the same address at the same time. This will inevitably lead to program crash during the analysis;

1 class A 2 {3 public: 4 A () 5 {6 m_Data = new char [100]; 7} 8 ~ A () 9 {10 delete [] m_Data; 11 m_Data = NULL; 12} 13 14 protected: 15 private: 16 char * m_Data; 17 }; 18 19 int _ tmain (int argc, _ TCHAR * argv []) 20 {21 A; 22 a B (A); 23 24 return 0; 25

Deep copy: You need to customize the copy function and save the pointer to a new memory segment. In this way, the structure will not crash;

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.