C ++ constructor is a special method used to initialize an object when creating an object, that is, assigning an initial value to the object member variable, whether it is a developer, Project Manager, or tester, having mastered C ++ constructor makes programming easier and easier.
The C ++ constructor initialization list starts with a colon, followed by a list of data members separated by commas (,). Each data member is followed by an initialized form in parentheses. For example:
- Class CExample {
-
- Public:
-
- Int;
-
- Float B;
-
- // List of constructor Initialization
-
- CExample (): a (0), B (8.8)
-
- {}
-
- // Internal assignment of constructor values
-
- CExample ()
-
- {
-
- A=0;
-
- B=8. 8;
-
- }
-
- };
In the above example, the results of the two C ++ constructors are the same. The constructor above uses the constructor in the initialization list to explicitly initialize class members. The constructor that does not use the initialization list assigns values to class members, no Explicit initialization is performed.
There is no major difference between initialization and assignment for built-in type members, like any of the above constructors. For non-built-in type member variables, we recommend that you use class constructor to initialize the list to avoid two constructor operations. However, you must use a constructor with an initialization list:
1. The member type is a class without a default constructor. If the display initialization type is not provided, the compiler implicitly uses the default constructor of the member type. If the class does not have the default constructor, the compiler will fail to use the default constructor.
2. const member or reference type member. Because the const object or reference type can only be initialized, they cannot be assigned a value. What is the meaning of initializing data members and assigning values to data members? What is the difference? First, the data members are classified by type and described as follows:
1. built-in data type, composite type pointer, reference)
In the member initialization list and in the C ++ constructor body, the performance and results are the same.
2. User-Defined type class type)
The results are the same, but the performance varies greatly. Because the data member objects of the class type have been constructed before they enter the function body, that is, the object construction work is performed at the member initialization list. Call the C ++ constructor after entering the function body.
- Analysis of C ++ constructor in C ++
- Exploring open-source C ++ Libraries
- Mode description of the VC ++ Development Environment
- In-depth description of C ++ open source program history
- How to Learn C ++ applications correctly
Assign values to constructed class objects and call a copy assignment operator. If not, assign values to members by default provided by the compiler. You may think that the above Code will first do m_y = I, then m_x = m_y, and finally they have the same value. But the compiler first initializes m_x and then m_y, because they are declared in this order.
The result is m_x, which will have an unpredictable value. There are two ways to avoid it. One is to declare members in the order you want them to be initialized, and the other is, if you decide to use the initialization list, these members are always listed in the order they are declared. This will help eliminate confusion.