Copy constructor: only one form parameter is used, and the form parameter is a reference to the object of this type (commonly used for const modification). Such constructor is called a copy constructor. Like the default constructor, the copy constructor can be implicitly called by the compiler. The copy constructor can be used:
- Initialize an object explicitly or implicitly based on another object of the same type
- Copy an object and pass it as a real parameter to a function.
- Copy an object when returning from the function
- Elements in the initialization sequence container
- Initialize array elements based on the element initialization list
Value assignment operator: Like a copy constructor, if you do not define your own value assignment operator, the compiler will combine them into one.
Next I wrote a program to explain the above description.
#include<iostream>#include<string>#include<vector>using namespace std;class Test{public:Test(){str="Kevin";x=10;cout<<"default constructor"<<endl;}//construction functionTest(const Test& t){str=t.str;x=t.x; cout<<"copy construction was used!"<<endl;}//copy constructor Test& operator=(const Test& t){str=t.str;x=t.x;cout<<"operator="<<endl;return *this;}private:string str;int x;};Test f(Test t){cout<<"f function"<<endl;return t;}main(){cout<<"Test t:"<<endl;Test t;cout<<endl<<"vector<Test> vec1(5):"<<endl;vector<Test> vec1(5);cout<<endl<<"vector<Test> vec2(5,t):"<<endl;vector<Test> vec2(5,t);cout<<endl<<"Test te[5]:"<<endl;Test te[5];cout<<endl<<"Test t2(t):"<<endl;Test t2(t);cout<<endl<<"f(t2):"<<endl;f(t2);cout<<endl<<""<<endl;Test* p1=new Test();*p1=t;Test* p2=&t;Test t3=t;Test t4;t4=t;}
The output result of the above program is:
default constructorvector<Test> vec1(5):default constructorcopy construction was used!copy construction was used!copy construction was used!copy construction was used!copy construction was used!vector<Test> vec2(5,t):copy construction was used!copy construction was used!copy construction was used!copy construction was used!copy construction was used!Test te[5]:default constructordefault constructordefault constructordefault constructordefault constructorTest t2(t):copy construction was used!f(t2):copy construction was used!f functioncopy construction was used!default constructoroperator=copy construction was used!default constructoroperator=
As can be seen from the 26th rows in the above program, when the replication constructor is used to initialize the elements of the container, if there is no initial value of the element, the compiler will first call the default constructor to create a temporary system, and then use the copy constructor to copy the temporary system to each element. When the initial value of an element is specified (as shown in 29 rows), the compiler directly copies it to each element.
According to the running results of Line 1 in the program, when defining an array of class types, the compiler will use the default constructor of the class to initialize each element.
According to the running results of row 38th in the program, when the class type is used as the form to participate in the return value, each of them has a copy. In C ++ primer, the description is as follows: "When the function parameter is of a non-reference type, the value of the real parameter is copied. Similarly, when a return value is made of a non-reference type, a copy of the value in the return statement is returned ". According to my understanding, "when neither the form parameter nor the return value is referenced, a copy occurs when you enter the function, and when you exit the function, a copy occurs ".
Lines 41st to 46th of the program are designed to illustrate the differences between the copy constructor and the value assignment operator. We can see that * P1 = T; this sentence uses the value assignment operator, T4 = T; this sentence also uses the value assignment operator, other forms of statements use the copy constructor. We can conclude that "from scratch is a copy, from there is a value ". How can this sentence be understood? As you can see, * P in the * P1 = T clause has already been declared and its initialization has been completed during * P1 declaration. In the * P1 = T clause, * P has been initialized. Therefore, * P1 = T applies the value assignment operator. Similarly, T4 in line 1 is a variable that has been declared and initialized. Therefore, the value assignment operator is used in statement t4 = T. In test T3 = T;
In such a statement, T3 is just declared and has not been initialized. the compiler is required to initialize T3 according to T content. This is exactly the role of the replication constructor.