Initialization Summary of member variables in C ++ class (revised version)

Source: Internet
Author: User

 

The original http://blog.csdn.net/jenghau/archive/2009/10/31/4752735.aspx from the link, but there are many problems in the middle, after my correction.

1. Common variables: assign values in constructors without considering the efficiency. For efficiency, you can perform this operation in the initialization list of the constructor.

Class ca
{
Public:
Int data;
Public:
CA ();
};

CA: Ca (): Data (0 )//...... #1 ...... Initialize the list
{
// Data = 0 ;//...... #1 ...... Assignment Method
};

2. Static static variables:

Static variables belong to all classes but not class objects. Therefore, no matter how many objects the class is instantiated, this variable has only one. In terms of this nature, it is somewhat similar to the uniqueness of global variables.

Class ca
{
Public:
Static int sum;
Public:
CA ();
};

Int CA: Sum = 0 ;//...... #2 ...... Class initialization

3. Const constant variable:
The const constant must be initialized upon declaration. Therefore, Initialization is required when a variable is created. It must be performed in the initialization list of the constructor.

Class ca
{
Public:
Const int Max;
Public:
CA ();
};

CA: Ca (): max (100)
{
......
}

4. Reference variables:
The referenced variable is similar to the const variable. It must be initialized at the time of creation. It must also be in the initialization list.
Class ca
{
Public:
Int Init;
Int & Counter;
......
Public:
CA ();
......
};

CA: Ca (): Counter (init)
{
......
}

5. Const static integral variable:
C ++ is privileged for const, static, and integer variables. It can be initialized directly in the class definition. Short works, but float does not.
Class ca
{
Public:
// Static const float fmin = 0.0; // only static const integral data members can be initialized within a class
Const static int Nmin = 0;
Public:
};

To sum up, there are four possible initialization conditions:
1. In the class definition, only the const and static and integral variables are involved.
2. In the class constructor initialization list, including common variables, const constants (excluding the first case), and reference variables.
3. initialized outside the class definition, including static variables. Because it is a unique variable of the class.
4. Common variables can be assigned within the constructor. Of course, this is inefficient.
5. the const data member (non-static) must be initialized in the const initialization list.
6. array members cannot be initialized in the initialization list.
7. The const static and static const are the same. Such variables can be directly initialized in the class definition or out of the class.

It illustrates a problem: the constant array cannot be defined in C ++! Because of the conflict between 5 and 6.

The construction order of class objects is as follows:
1. allocate memory. when calling the constructor, the data members are initialized implicitly/displayed.
2. After Entering the constructor, execute general calculations in the constructor.

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.