A copy constructor is a special member function in a class. The copy constructor is also a constructor that has the same function name as the class name, no return type, and its role is to initialize the members of the class and allocate storage space for the object's construction, except that the parameters of the copy constructor are only one. and must be a reference to the class object.
The copy constructor is used only when one object initializes another object, so let's start with the simple words:
int a = 10;
int b = A;
cout << a<< b << Endl;
In the code snippet, use integer variable A to initialize B, at which point B's value is the value of a. Similarly:
[cpp] View Plain copy #include <iostream> using namespace std; class a{ public: a (int x) { a = x; } void show () { cout<< "a = " <<a<<endl; } ~a () {} private: int a; }; Void main () { int m = 10; a ob1 (m); ob1.show (); &NBSP;&NBSP;&NBSP;&NBSP;A&NBSP;OB2&NBSP;=&NBSP;OB1;&NBSP;&NBSP;//A&NBSP;OB2 (OB1); ob2.show (); }
In the code snippet, use object Ob1 to initialize object Ob2, at which point the value of member A of OB2 is initialized by OB1 member A, that is, ob2.a = ob1.a; This procedure is done using the copy constructor of the class, but in the code above we do not see the copy constructor, The same is done with the copy work, this is why. Because when a class does not have a custom copy constructor, the system automatically provides a default copy constructor to complete the replication work. So, to illustrate the situation, in the general case (take the code above for example), let's define a copy constructor like the default copy constructor of the system, and see how it works inside:
[cpp] View Plain copy #include <iostream> using namespace std; class a{ public: a (int x) //Constructors { a = x; } void show ( ) { cout<< "a = " <<a<<endl; } a (A&NBSP;&R_OB)//This is the custom copy constructor { cout << "Using copy constructors" << endl; a = r_ob.a;//This sentence can not be completed if you remove the copy work } ~a () {} private: int a; }; void main () { int m = 10; a ob1 (m); ob1.show (); &NBSP;A&NBSP;OB2&NBSP;=&NBSP;OB1;&NBSP;&NBSP;//A&NBSP;OB2 (OB1); ob2.show (); }
A (a &r_ob) in the code above is our custom copy constructor, the name of the copy constructor must be the same as the class name, and the form parameter of the function is a reference of this type and must be a reference.
When you initialize another newly constructed object with a custom class-type object that has already been initialized, the copy constructor is invoked automatically, and if you do not have a custom copy constructor, the system will provide a default copy constructor to complete the process, and the copy core statement of the above code is through a (a &r_ob) a = R_OB.A in the copy constructor. If you remove this code, the A property of the B object will get an unknown random value. Let's talk about shallow copies and deep copies. As far as the above code is concerned, many people will ask, since the system automatically provides a default copy constructor to handle replication, we do not need to customize the copy constructor Ah, yes, in general, this is not necessary, but in some cases, the members of the class body is required to dynamically open heap memory, If we do not customize the copy constructor and let the system handle itself, it will cause confusion over the ownership of the heap memory. Imagine that a heap of addresses that had been opened up was originally object A, as a result of the replication process, B object acquisition is a already open heap address, once the program generated destructor, release heap, the computer is impossible to know this address is really belong to, when the continuous occurrence of two of the destructor when the run error occurred.
[cpp] View plain copy #include <iostream> #include <string.h> using namespace std; class a{ public: a (char *n,char *ptr) { strcpy (name,n); Assignment of //static space address = new char[strlen (PTR) + 1]; // Assignment after dynamic space application if (address != null) strcpy (address,ptr); } void show () { cout<< "Name is:" <<name< <endl; cout<< "Address is:" <<address <<endl; } a (A&NBSP;&R_OB)// This is the custom copy constructor { cout<< "Using copy constructors" <<endl; strcpy (name,r_ob.name); address = New char[strlen (r_ob.address) + 1]; //assignment after dynamic space application if (address != null) strcpy (address,r_ob.address); } ~a () { cout<< "Call destructor" <<endl; delete []address; } private: char name[20]; char *address; }; Void main () { a ob1 ("John", "Nanchang"); ob1.show (); A ob2 = ob1; A&NBSP;OB2 (OB1); ob2.show (); }
The code above demonstrates the problem of deep copy, the address attribute of the object OB2 has taken the new memory to avoid the memory attribution is caused by the destruction of space time error, finally must mention, for the above procedural explanation is not much, is to hope that the reader itself running procedures to observe changes, and then a profound understanding. (Readers can try to annotate the copy constructor to see the results of the running program, or you can observe the program's execution in a single step)
The definition of deep copy and shallow copy can be easily understood as: If a class has resources (heaps, or other system resources), the process can be called a deep copy when the object of the class is replicated, whereas the object has resources but the copy process does not replicate the resource as a shallow copy. In particular, it is necessary to note that when a shallow copy of a resource causes a resource to become ambiguous when releasing the resource, the program runs an error.