Reprinted -c ++ deep copy and shallow copy

Reprinted from: https://blog.csdn.net/u010700335/article/details/39830425

A copy of C ++ classes in two ways: deep copy and shallow copy: When a class assignment equal sign appears, that will call the copy function
A: difference between the two 
1 undefined in the case of displaying the copy constructor, the system will call the default copy function - that is shallow copy, it can complete the members one by one copy. When no pointer data member, shallow copy is feasible; but when there is a pointer data member, if a simple shallow copy, the two types of pointers point to the same address, when the end of the object, two calls the destructor, a pointer leading to the phenomenon of the suspension, therefore, this time, must be deep copy. 
Difference deep copy and shallow copy 2 lies deep copy would otherwise apply to store data in the heap memory, so that it solves the problem of suspension pointer. In short, when there is a data member pointer, you must use a deep copy.

Explained with an example of two
c ++ default copy constructor is shallow copy 
shallow copy is simple assignment between data members of the object, if you do not design a class without providing its copy constructor, when using such an object to an object to make when the assignment process is executed shallow copy, such as:
class A 
{ 
    public : 
    A ( int the _data): Data (the _data)} { 
    A () {} 
    Private : 
     int Data; 
 }; 
int main () 
{ 
    A A ( . 5 ), A = B; // only data members between assignment 
}
This is a b = a; shallow copy is performed after the completion of sentence b.data = 5; 
if no other object resources (eg: heap, files, system resources, etc.), the deep and shallow copy copy no difference, 
However, when the object of these resources, examples:
class A 
{ 
    public : 
    A ( int _size): size (_size) 
    { 
        Data = new new  int [size]; 
    } // If wherein some dynamically allocated memory 
    A () {};
      ~ A () 
    { 
        Delete [] Data ; 
    } // release resources destructor 
    Private : 
     int * Data;
     int size; 
} 
int main () 
{ 
    A A ( . 5 ), A = B; // Note that a 
}
Where b = a will result in undefined behavior, because the copy constructor class A is generated by the compiler, so that b = a a shallow copy execution process. I said shallow copy is a simple assignment between data objects, such as: 
b.size = a.size; 
b.data = a.data; // the Oops! 
Where b and a data pointer to a pointer to the heap the same memory, when a and b destructor, b first data memory which point to a dynamically allocated released, then this in turn has been released through the memory when a destructor released once again. The result is undefined dynamic memory to perform the same piece twice or more to release, so it will lead to memory leaks or crashes. 
So here we need a deep copy to solve this problem, a deep copy refers to when a copy of the object has references to other resources (such as heap, file systems, etc.) (reference may be a pointer or reference), another open object a new resource, rather than a copy of the object has a pointer to other resources referenced or cited were simple assignment. Such as:
class A 
{ 
    public : 
    A ( int _size): size (_size) 
    { 
        Data = new new  int [size]; 
    } // If wherein some dynamically allocated memory 
    A () {}; 
    A ( const A & READ_A): size ( _A.size) 
    { 
        Data = new new  int [size]; 
    } // deep copy 
    ~ A () 
    { 
        Delete [] Data; 
    } // release resources destructor 
    Private : 
     int * Data; 
     int size; 
 } 
int main () 
{ 
    A A ( . 5 ), A = B; // this is no problem 
}
Summary: When deep and shallow copy copy difference is contain other objects in the object referenced state, when copying an object, the object if you need to copy the object reference, it is a deep copy, otherwise it is shallow copy.

Guess you like

Origin www.cnblogs.com/hellowooorld/p/11259560.html