The C ++ programming language also has the object-oriented feature. For example, class members provided for classes in C ++. We will introduce the knowledge of C ++ class members in detail here, hoping to help you understand this knowledge.
- How to Implement C ++ print address information
- Detailed description of how C ++ generates random numbers
- Practical application skills of C ++ linked list operations
- A Brief Introduction to the C ++ Operating Mechanism
- Basic concepts of C ++ cyclic statements
C ++ provides the initialization list for Class C ++ class members.
The construction order of class objects is as follows:
1. Any member variables in the class cannot be initialized during definition.
2. Generally, data members can be initialized in the constructor.
3. the const data member must be initialized in the const initialization list.
4. static should be initialized outside the class definition.
5. array members cannot be initialized in the initialization list.
6. You cannot specify an Explicit initialization for the array.
These six items indicate a problem: the constant array cannot be defined in C ++! Because of the conflict between 3 and 5. Can't this happen? No way, I had to turn to static data members.
At this point, my problem is solved. But I also want to take the opportunity to review C ++ class initialization:
1. Initialization list: CSomeClass (): x (0), y (1 ){}
2. Out-of-class initialization: int CSomeClass: myVar = 3;
3. the const constant definition must be initialized, and the initialization list should be used in the C ++ class;
4. The C ++ class cannot define a constant array.
For const, review the constant pointer:
If the const is on the left side of the asterisk, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant. If the const is on the right side of the asterisk, the const is used to modify the pointer, that is, the pointer itself is a constant.
In the C ++ class, you must do the following:
1. You must use the initialization list to initialize any const, reference type members, and any member of the class type without the default constructor.
2. Class Members cannot be initialized during definition.
3. the initialization sequence of C ++ class members is irrelevant to the order in which the member variables are located in the constructor. The sequence of member variables defined in the class is related.
The following example is incorrect:
- class x
- {
- int i;
- int j;
- public :
- x( int value):j(value),i(j)
- {
- }
- }
For the code above, because I was first initialized and I was first defined relative to j), but during I initialization, j was not initialized, so the execution encountered a problem.
The concepts of C ++ class members are introduced here.