Application Methods for C ++ class members

Source: Internet
Author: User
Tags constant definition

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:

 
 
  1. class x  
  2. {  
  3. int i;   
  4. int j;  
  5. public :  
  6. x( int value):j(value),i(j)  
  7. {  
  8. }  

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.