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}