Light replication and deep Replication

Source: Internet
Author: User

Shallow Replication refers to the replication behavior based entirely on the data content in the memory, that is, it faithfully copies the data to the target memory region regardless of the original data type. The default copying constructor and the value assignment function of the C ++ compiler are all shortest copies. For example:

Class A {public: A (int I): m (I) {} PRIVATE: int M ;}; A (2); a B ();

Shallow replication has a problem with pointers: the pointer is copied instead of the object referred to by the pointer. That is, the content of pointer-type data is a memory address, if it is copied as is, two different pointer members point to the same memory area, for example, P2 = p1; pointer P2 points to the space pointed to by pointer P1. If the space does not exist, so the point of P2 is meaningless, and deep replication is required for processing.

Deep Replication refers to re-opening a memory space if the content to be copied is a pointer, copy the content of the memory space that the original Pointer Points to the new memory space that the target Pointer Points. In this way, the two pointers point to the same memory space value, but they are independent memory space and do not interfere with each other. For example:

String: string (const string & Str) // copy the constructor {int Len = strlen (Str. m_data); m_data = new char [Len + 1]; // apply for an array strcpy (m_data, str. m_data); // copy the array}

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.