Bitwise COPY Copies the address (also called the shortest copy), while value COPY Copies the content (deep copy ). Deep copy and shallow copy can be simply understood as: If a class has resources, when the objects of this class are copied, the resources are re-allocated. This process is a deep copy, and vice versa, if no resources are re-allocated, it is a small copy.
Bit copy, and "bitwise assignment" means to copy the memory image of an object to another object by bit. The so-called value copy refers, copy the value of the original object to the new object. When "bitwise assignment" is used, the memory image of the object is directly copied to another object, so that the two objects direct to the same memory region. when an object is released, the pointer of another object will become a wild pointer (suspension pointer ). In this case, you should write operator = and copy constructor to copy values.
.
The default copy constructor and the default value assignment function both adopt the bitcopy method instead of the value COPY method. If the class contains pointer variables, these two functions are doomed to errors.
When an initialized custom class object is used to initialize another newly constructed object, the copy constructor is automatically called. That is to say, when the class object needs to be copied, the copy constructor will be called. The copy constructor is called in the following cases:
- An object passes in the function body as a value.
- An object is returned from the function by passing values.
- An object needs to be initialized through another object.
If a copy constructor is not explicitly declared in the class, the compiler automatically generates a default copy constructor, which completes the bitwise copy between objects. Bit copy is also called the shortest copy. Custom copy constructor is a good programming style. It can prevent the compiler from forming a default copy constructor and improve the source code efficiency.
If there is no custom replication constructor, the system will create the default replication constructor. However, the default replication constructor created by the system will only execute "bit copy ", values of the Data members of the Copied object are assigned to the newly created object. If the data member of this class has pointer members, the pointer of the new object is directed to the same address as the pointer of the Copied object. When you delete the pointer, duplicate delete operations may occur. The following is a classic example:
Class string {public: string (const char * Ch = NULL); // default constructor string (const string & Str); // copy constructor ~ String (void); string & operator = (const string & Str); // value assignment function PRIVATE: char * m_data ;};
If you use string as an example to define stra and strb
Int main () {string stra ("hello"); string strb ("world"); strb = stra; // As a result, the strb and stra pointers point to the same address. // when the function ends the destructor, // The return 0 for the same address is deleted twice ;}
If you do not write the copy constructor and value assignment function, the compiler automatically generates the default function in the "bit copy" mode. If the class contains pointer variables, the two default functions are implicitly incorrect. Take the two objects of the string type as an example. Assume that the content of stra. m_data is "hello" and the content of strb. m_data is "world ".
Now, stra is assigned to strb. The "bit copy" of the default value assignment function means that strb. m_data = stra. m_data is executed. This will cause three errors:
- The original memory of strb. m_data is not released, causing memory leakage;
- Strb. m_data and stra. m_data point to the same memory. either of the following changes will affect the other;
- M_data is released twice when the object is destructed.
If the compiler does not actively write the copy function and value assignment function, it will automatically generate the default function in BIT copy mode.