I haven't touched C ++ for a long time. The next project will start to use C ++, so many things need to be picked up and checked again. Take notes from today for future reference. Let's get down to the point. Let's start with the initialization list of the constructor. I have refined this knowledge point into three questions: 1. Why is there an initialization list? What is the difference between it and the initialization in the constructor. 2. the initialization order. 3. Pay attention to the details.
Let's talk about the first question first. We have two things that must be completed in the initialization list. One is the variable modified by the const and the other is the reference. I will not elaborate on this point. You can find all the materials you have read. The following describes the class members.
Class Test1
{
Public:
Test1 ()
{
Cout <"Test1 default constructor" <endl;
}
Test1 (int I)
{
Cout <"Test1 value constructor" <endl;
}
Test1 (const Test1 & obj)
{
Cout <"Test1 copy constructor" <endl;
}
Test1 & operator = (const Test1 & obj)
{
Cout <"Test1 = constructor" <endl;
Return * this;
}
~ Test1 ()
{
Cout <"Test1 destructor" <endl;
}
};
I define a class and then define another class, which contains Test1
Class Test2
{
Public:
Test2 ()
{
T1 = Test1 (1 );
}
Private:
Test1 t1;
};
Initialize Test1 In the constructor. Let's take a look at the running result.
Test1 default constructor
Test1 value constructor
Test1 = constructor
Test1 destructor
Test1 destructor
Let's analyze the output. Test1 default constructor, which indicates that the t1 member has been initialized before entering the Test1 constructor and the non-argument constructor is called. Test1 value constructor: the object created by Test1 (1. Test1 = constructor. This indicates that the copy constructor is used. Test1 destructor indicates the analysis structure of the temporary object Test1 (1, test1 destructor indicates the structure of the member object t1. From the above results, in the constructor t1 = Test1 (1); in fact, it is not a true initialization, but a copy assignment. Before entering the constructor, the class members are initialized by the default constructor. If Test1 is a large object, this will cause performance overhead. Therefore, this is also one of the reasons for using the initialization list.
Next, let's talk about the sequence. The simple principle is that the initialization list is prior to the constructor, and the initialization list is declared in the order of variables. Let's take a look at the example below.
Class Test3 ()
{
Public:
Test3 (int x, int y, int z): _ z (z), _ y (y)
{
_ X = x;
}
Private:
Int _ x, _ y, _ z;
};
According to the preceding statement, the assignment order is _ y, _ z, and _ x.
The third problem is that each member can only appear in the initialization list once.
Class Test3 www.2cto.com
{
Public:
Test3 (int x, int y, int z): _ z (z), _ y (y), _ z (x)
{
_ X = x;
}
Private:
Int _ x, _ y, _ z;
};
For example, this is a problem. _ Z is initialized twice.
Author: yg2133