March mid-June to attend the lecture, did the X soft Company's C + + Pen test questions, which has a "default copy of the constructor function", because for a long time did not review C + + basic knowledge, at that time even basic concepts are not to come. As a result, began to pick up the previous Tan Haoqiang C + + review up, now the book is about to chew up, feel a lot of harvest. Like practicing martial arts, cheats although important, but more important is the deep internal force and solid basic skills. Constructors in C + +
The constructors in C + + can be grouped into 4 categories:
(1) Default constructor. In the case of the student class, the default constructor's prototype is
Student ()//No parameters
(2) Initialization constructor
Student (int num,int age);//have parameters
(3) Copy (copy) constructors
Student (student&);//formal parameter is a reference to this class object
(4) Transformation constructor
Student (int r);//other type variable with only one parameter default and initialization constructor
The default constructor and the initialization constructor complete the initialization of the object when defining the object of the class.
Class Student
{public
:
//default constructor
Student ()
{
num=1001;
age=18;
}
Initialize constructor
Student (int n,int a): num (n), age (a) {}
private:
int num;
int age;
};
int main ()
{
///initializes object with default constructor S1
Student S1;
Initializes the object with the initialization constructor S2
Student S2 (1002,18);
return 0;
}
copy (copy) constructorsCopy constructors for copying objects of this class
Student S2 (1002,1008);
Student S3 (S2);//Copy Object S2 to S3. Note that the concepts of replication and assignment are different.
The following situation is called assignment, and the copy constructor is not invoked.
Student S4;
s4=s2;//This kind of situation is called the assignment value, oneself experience
Most of the time, in the class we don't declare a copy constructor, but C + + automatically generates a copy constructor for us, as follows:
Student (Student &b)
{
this.x=b.x;
this.y=b.y;
}
As the code shows, its purpose is to copy an existing object B to the object that invoked the copy constructor. Specifically, the copy constructor is invoked when a situation occurs:
Creates a new object by using the copied method. When the parameters of a function are objects of a class. (This is similar to the normal type of formal parameters, to copy an argument to a function) the return value of the function is the object of the class, the object defined in the function, the message at the end of the function, the copy constructor needs to be called, a temporary object is created, and the temporary object is returned to the object that called the function. The default copy constructor, in some cases there will be problems, want to further study can be their own Baidu.
Transformation ConstructorsTransformation constructors are used to implicitly convert other types of variables to this class object. The following transformation constructor, which converts an int type of
RConverted to
StudentObject of the type, with an age of R,num 1004.
Student (int r)
{
int num=1004;
int age= r;
}
where the transformation constructor can be used. If the + operator is overloaded, two objects of the student class can be added, and the result is the sum of the member variables of the two objects.
Student S1 (01,18);
Student S2 (02,20);
S1+S2; The value is S1.age + s2.age = 18+20=36.
So what about S1+19 (class objects are added directly to int).
Since we have defined the transformation constructor, then s1+19, if the procedure: the preferred call + operator, finds that 19 is not an object of the student class, but instead an int type and then calls the transformation constructor, and 19 becomes student (19), then the addition can now be performed. The value is s1.age+ (tempstudentobject). age=18+19 = 37
Here is just a very shallow talk about the basic concepts of these types of constructors, if you want to learn more, there are many references online.