In C + +, it is often a matter of shallow copying and deep copying of class objects, which is also an error-prone place.
Find relevant information, about shallow copy and deep copy is defined as: Copy the class when the bitwise copy, that is, the value of the data members of an object is copied to the target object. When a pointer type data member is involved in a class, a pointer hanging problem is often generated.
class a{public: int* A;}; A A1; A B1=a1;
B1=A1 performs a shallow copy, at which point a1.a and b1.a refer to the same memory address if there is a release of memory in the destructor. A memory access exception occurs. Because a piece of memory space will be released two times!
The reference code understands:
#include <iostream>#include<string.h>using namespacestd;classperson{Private: Char*name; intAge ; Public: Person (Const Char*name,intAge ) {Name=New Char[Strlen (Name) +1]; strcpy (Name,name); Age=Age ; cout<<"construct ..."<<Endl; } ~Person () {cout<<"Destruct ..."<<age<<Endl; Deletename; } voiddispaly () {cout<<name<<" "<<age<<Endl; } voidSetage (intx) { age=x; }};intMain () {person P1 ("Jack", at); Person P2=P1; P1.setage (Ten);//P2.setage (a);p1.dispaly (); P2.dispaly (); return 0;}
From the running result we can see that the program just called the constructor once, but it performed two destructors, and did not meet the expected expectations. The object P2=P1 performs a shallow copy, the pointer name in P2 is the same address that is pointed to in pointer name and P1, and because there is no constructor defined, the system takes the default copy constructor when the P2=P1 is executed (the default copy constructor does not reallocate new memory space for new objects). That is, a bitwise copy of the value of each data member in the P1 is copied to the corresponding member of the P2, resulting in p1.name=p2.name (pointing to the same memory), at which time the constructor of the class person is executed only once.
When the Last "}" of the main () function is encountered, the execution of the destructor, which is executed by the destructor, is performed, so the P2 destructor is executed first. At this time the system will p2.name point to the storage unit released, in the execution of the P1 destructor, P1.name point to the memory unit is released, which will cause the same piece of memory space is freed two times, resulting in errors, p1.name is also a hanging pointer.
To solve this problem, a deep copy of the P1 is required, that is, to construct a copy function that allows the object P2 to acquire new memory space when copying P1.
#include <iostream>#include<string.h>using namespacestd;classperson{Private: Char*name; intAge ; Public: Person (Const Char*name,intAge)//constructor Function{Name=New Char[Strlen (Name) +1]; strcpy (Name,name); Age=Age ; cout<<"construct ..."<<Endl; } person (ConstPerson &p)//copy Constructor{Name=New Char[Strlen (P.name) +1]; strcpy (Name,p.name); Age=P.age; cout<<"Copy construct ..."<<Endl; } ~Person () {cout<<"Destruct ..."<<age<<Endl; Deletename; } voiddispaly () {cout<<name<<" "<<age<<Endl; } voidSetage (intx) { age=x; }};intMain () {person P1 ("Jack", at); Person P2=P1; P1.setage (Ten);//P2.setage (a);p1.dispaly (); P2.dispaly (); return 0;}
from the run results you can see that the expected expectations are met, and from the copy constructor definition you can know that the copy of the class object has been re-allocated to the new object for the new memory unit. deep copy and shallow copy can be simply understood as: If a class has resources, when the object of this class has a replication process, the resource is redistributed, the process is a deep copy, conversely, no redistribution of resources, is a shallow copy.
define copy constructors in the form of: X (x& x) {a=new??????}
When do I use the copy function?
A . An object is passed into the function body in the way of value passing;
B. An object is returned from the function in the way that the value is passed;C. An object needs to be initialized by another object.
Shallow copy and deep copy in C + +