Summary of initialization of member variables in C + + classes

Source: Internet
Author: User

1. Common variables:

You can assign a value in a constructor without considering the efficiency. Consider the efficiency that can be done in the initialization list of the re-constructor function.

1classCa
2{
3 Public:
4intData
5 Public:
6CA ();
7};
8
9CA::CA ():d ATA (0)//.... #1 ..... Initialize list mode
Ten{
One//data = 0;//.... #1 ..... How to assign a value
A};

2. static variable:

the static variable belongs to the class and not to the object of the class, so there is only one variable regardless of how many objects the class is instantiated. Understanding in this nature is somewhat analogous to the uniqueness of global variables.  

1classCa
2{
3 Public:
4StaticintSum
5
6 Public:
7CA ();
8};
9intca::sum=0;//.... #2 ..... Initializing outside of the class

3,const constant Volume:

Const constants need to be initialized at the time of declaration. It is therefore necessary to initialize the variable when it is created. Generally used in the initialization list of constructors.

1 classCa
2{
3 Public:
4 Const intMax
5 Public:
6CA ();
7};
8
9CA::CA (): Max ( -)
Ten{
One}

4.Reference reference variable:

The reference variable is similar to the const variable. It needs to be initialized at the time of creation. is also done in the initialization list. But you need to be careful with the reference type.   

1 classCa
2{
3 Public:
4 intInit
5 int& Counter;
6 Public:
7CA ();
8};
9
TenCA::CA (): Counter (&init)
One{
A}

5.const static Integral variable :

C + + is privileged for both const and static as well as shaping variables (but different compilers may have different support, and VC 6 doesn't seem to support it). can be initialized directly in the definition of a class. Short can, but float's not possible oh.

1 // example float type can only be initialized outside the class
2 // Const FLOAT ca::fmin = 3.14;
3 classCa
4{
5 Public:
6 // static const float fmin = 0.0; // Only the static const integral data members can be initialized within a class
7 Const Static intNmin = 0;
8......
9 Public:
Ten......
One};

In summary, there are four places where you can initialize:

1, in the definition of the class, only the const and static and integral variables.

2. In the constructor initialization list of the class, include the Const object and the Reference object.

3. Initialized outside the definition of the class, including the static variable. Because it is the only variable that belongs to the class.

4, ordinary variables can be in the interior of the constructor, through the assignment method. Of course, this is inefficient.

 

Summary of initialization of member variables in C + + classes

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.