The copy constructor, as the name implies, creates a new object by copying the object. There are two prototypes of the copy constructor (we continue to use the book class to illustrate the copy constructor prototype):
Book (Book &b); Book (theconst book &b); The following prototype specifies that the copied object must not be modified when a new object is created
Both of these prototypes are references to the book class object. The following prototype specifies that the copied object must not be modified when a new object is created. It is not allowed if the parameter of the copy constructor is not a reference to the object . As in the following constructor form, it cannot be compiled.
// unable to compile past
Why is the parameter of the copy constructor necessarily a reference to the object? We can think about it, if it is not a reference, but passing the argument to the parameter by means of a value, which is itself going through the process of copying the object, and the copy of the object must call the copy constructor, so that a dead loop is formed, and there is no solution. So the parameter of the copy constructor must be a reference to the object.
The copy constructor can have other parameters in addition to the object reference. However, other parameters must be given a default value. For example, here is how the copy constructor is declared.
Book (const5.0);
If the designer of the class does not declare a copy constructor that is displayed in the class, the system automatically generates a copy constructor for the class, and the automatically generated copy constructor functions simply to copy all member variable one by one of the source object to the currently created object.
classbook{ Public: Book () {} Book&C); Book (CharADoublep =5.0); voiddisplay ();Private: DoublePrice ; Char*title;}; Book::book ( Book&b) { Price=B.price; Title=B.title;} Book::book (CharADoublep) {Title=A; Price=p;}voidBook ::d isplay () {cout<<" The price of"<<title<<"Is $"<<price<<Endl;}intMain () { Chara[5] = {0,1,1,1,1}; Book Math{a,5}; Book Yuwen (math); //Book ::p rice =8;//Dandy cannot be compiled successfully because the price variable is private and cannot be accessed outside the member classYuwen.display (); Math.display (); return 0;}
In this example, a copy constructor is declared in the book class, and the copy constructor is the same as the copy constructor function generated by the system by default, and only the corresponding copy function of the data member is implemented.
Understand the declaration and definition of copy constructor, we look at when we design the class, when we need to design the copy constructor, we first look at the following example, I believe after reading will have a certain understanding, and then to reveal the answer.
#include <iostream>using namespacestd;classarray{ Public: Array () {length=0; num =NULL;}; Array (intAintN); voidSetnum (intValueintindex); int*getaddress (); intGetLength () {returnlength;} voiddisplay ();Private: intlength; int*num;}; Array::array (int*a,intN) {num=New int[n]; Length=N; for(intI=0; i<n; i++) Num[i]=a[i];}voidArray::setnum (intValueintindex) { if(Index <length) Num[index]=value; Elsecout<<"index out of range!"<<Endl;}voidArray::d isplay () { for(intI=0; i<length; i++) cout<<num[i]<<" "; cout<<Endl;}int*array::getaddress () {returnnum;}intMain () {inta[5] = {1,2,3,4,5}; Array arr1 (A,5); Arr1.display (); Array arr2 (ARR1); Arr2.display (); Arr2.setnum (8,2); Arr2.display (); Arr1.display (); cout<<arr1.getaddress () <<" "<<arr2.getaddress () <<Endl; return 0;}
The results of the program run as follows:
1 2 3) 4 5
1 2 3) 4 5
1 2 8) 4 5
1 2 8) 4 5
00331f58 00331f58
In this example, we redefine an array class, which can be understood as an array class, in which we define two member variables: the Shaping pointer num and the length of the arrays.
A default constructor is defined in the class, and a parameter constructor is declared. The default constructor is simple, and the parameter constructor is used to copy all the existing arrays to the class object.
In addition to the two constructors, we also define four member functions, one for the Setnum function that modifies the values in the array, a display function that prints all the elements in the array, a function getaddress that returns the first address of the array, and a function getlength that returns the length of the array. Except for the default constructor and the GetLength function, all functions are defined outside the class.
#include <iostream>using namespacestd; classArray { Public: Array () {length=0; num =NULL;}; Array (intAintN); Array (Array&a); voidSetnum (intValueintindex); int*getaddress (); voiddisplay (); intGetLength () {returnlength;} Private: intlength; int*num; }; Array::array (Array&a) {if(A.num! =NULL) {Length=a.length; Num=New int[length]; for(intI=0; i<length; i++) Num[i]=A.num[i]; } Else{length=0; Num=0; }} array::array (int*a,intn) {num=New int[n]; Length=N; for(intI=0; i<n; i++) Num[i]=A[i]; } voidArray::setnum (intValueintindex) { if(Index <length) Num[index]=value; Elsecout<<"index out of range!"<<Endl; } voidArray::d isplay () { for(intI=0; i<length; i++) cout<<num[i]<<" "; cout<<Endl; } int*array::getaddress () {returnnum; } intMain () {inta[5] = {1,2,3,4,5}; Array arr1 (A,5); Arr1.display (); Array arr2 (ARR1); Arr2.display (); Arr2.setnum (8,2); Arr2.display (); Arr1.display (); cout<<arr1.getaddress () <<" "<<arr2.getaddress () <<Endl; return 0; }
See how this two is different. The address of the object has changed.
copy of C + + constructor function