Many people are confused about the center Constructor's sleep initialization. After the colon is initialized, the difference between them is not clear, I want to discuss my understanding and opinions on this issue with you.
In the program, variables are defined and initialized in two forms: one is our traditional initialization form, that is, the value assignment operator, and the other is the brackets assignment, such:
Int A = 10;
Char B = 'R'; // value assignment operator
Int A (10 );/
Char B ('R'); // brackets
The above definition and initialization form are correct and can be compiled, but the value assignment in parentheses can only be defined and initialized, and cannot be used after the definition of variables, this is different from the value assignment operator, for example:
(1)
Int A; // define a variable first
......
A = 10; // assign values as needed
(2)
Int B; // define a variable first
......
B (10); // same as (1), assign values as needed
(1) It can be compiled to define a variable A but it is not initialized. When variable A is required, 10 is assigned to a through the value assignment operator, and in (2) in, 10 is assigned to B through parentheses, but the compilation system considers
This is a function call. The name of the function is B and 10 is the actual parameter. Therefore, the compilation is incorrect. Therefore, the value assignment in parentheses is only used to define variables and initialize them.
Download dl.bitscn.com
Now let's look at the problems of colon initialization and function initialization in constructor. Class constructor is used to call a class object to construct the data members of this class object when creating a class object, first, we need to allocate memory space for this data member. Second, we need to initialize the function data member and construct the data member in the Order stated by the data member in the class.
The difference between colon initialization and function body Initialization is:
The colon (:) Initialization is performed when a data member is allocated memory space. That is to say, if a data member is assigned a value expression after the colon (this expression must be a value expression in parentheses ), after the memory space is allocated, the data member is assigned a value before entering the function body, that is, the data member is initialized and the function body has not yet been executed.
Initialization in a function is performed only after all data members are allocated memory space.
This is advantageous. Some data members need to initialize the function body before calling the constructor, such as referencing data members, constant data members, and object data members, take a look at the following program:
Class student
{Public:
Student ()
.
.
.
Protected:
Const int;
Int & B;
China Network Management Alliance bitscn.com
}
Student: Student (int I, Int J)
{
A = I;
B = J;
}
The student class has two data members: one is a constant data member, the other is a reference data member, and the two data members are initialized in the constructor, but this cannot be compiled, because a constant must be assigned a value during initialization, its value cannot be changed. Like a constant, a value must be assigned for initialization. After a reference is defined, it is associated with the referenced object and cannot be assigned a value. So C
+ + ":" The post-initialization mechanism makes reference and constant data members possible. The student class constructor should be:
Student: Student (int I, Int J): A (I), B (j ){}
In the following program:
Class teach
{
Public:
Teach (char * P = "name", int A = 0)
.
.
.
Protected:
Char name [30];
Int age;
}
Teach: Teach (char * P, int)
{
Strcopy (name, P );
Age = A; Network Management Forum bbs_bitscn_com
Cout> Name> Endl;
}
Class student
{
Public:
Student (char * P = "name ");
.
.
.
Protected;
Char name [30];
Teach teacher;
};
Student: Student (char * P)
{
Strcopy (name, P );
Cont> Name> Endl;
}
In the above program, the compilation system will tell you that the default constructor is missing for the teacher Class Object, because the default constructor is not defined in the teach class. How can a constructor with parameters be constructed? Assign values using the colon as mentioned above. The constructor should be:
Student: Student (char * P, char * PL, int AG): Teacher (PL, AG)
{
Strcopy (name, P );
Cont> Name> Endl;
}
That is to say, if a class object is a data member of another class without a default constructor, the data member should be initialized after the colon. This can contain parameters. In the class definition, for example:
Network Management bitscn_com
Protected;
Char name [30];
Teach teacher
Class Object cannot contain parameters because it is only declared.
Therefore, this mechanism is added in C ++, which is required for object-oriented programming. I don't know. If you do not understand, please refer to the relevant information.
Ref: http://blog.csdn.net/yiyunoo/article/details/2452363