Initialization methods for special member variables (constant variables, references, static) in C + + classes-additional content to be added

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/zhoug2020/archive/2012/08/31/2665451.html

Some member variables have a special data type, and they are initialized differently from the member variables of the normal data type. These special types of member variables include:

    • A. References
    • B. Constants
    • C. Static
    • D. Static constants (integer type)
    • E. Static constants (non-integral type)

Constants and references, which must be initialized through a parameter list.
The initialization of static member variables is also quite special, it is initialized outside the class and can no longer carry the static keyword, its essence is at the end of the text.

Refer to the following code and its comments:

#include <iostream>using namespacestd;classbclass{ Public: Bclass (): I (1), CI (2), RI (i) {}//for constant type member variables and reference member variables, you must initialize them by using a parameterized list//Ordinary member variables can also be placed in the function body, but the essence is not initialized, but a common operation operation--assignment, efficiency is lowPrivate: intI//Normal member variables Const intCi//Constant member variables int&ri;//referencing member variables Static intSi//Static member variables//static int si2 = 100; //error: Only static constant member variables can be initialized like this Static Const intCsI//static constant member variable Static Const intCsi2 = -;//initialization of static constant member variables (Integral type) (1) Static Const DoubleCSD//static constant member variable (non-integral type)//static const Double CSD2 = 99.9; //Error: Only static const integer data members can be initialized in a class};//Note the following three lines: no more staticintBclass::si =0;//initialization of static member variables (Integral type)Const intBCLASS::CSI =1;//initialization of static constant member variables (Integral type)Const DoubleBCLASS::CSD =99.9;//initialization of static constant member variables (non-integral type)//in the initialization (1) of Csi2, according to famous master Stanley B.lippman, the following line is required. //but in VC2003 the following line will produce an error, and in VC2005, the following row is optional, which is related to the compiler. Const intBclass::csi2;intMain () {Bclass B; return 0;}

---------------------------------------------------------------------------------------------

Static members belong to the class scope, but not to the class object, and as with normal static variables, the program allocates memory and initializes it as soon as it runs, and the lifecycle and program are consistent.
Therefore, it is obviously unreasonable to initialize the static variable in the constructor of the class.
A static member is actually the same as a global variable, except that the compiler restricts its use to the class scope (not the class object, which is not part of the class object), and is initialized outside of the class's definition, not the scope of the class.

Initialization methods for special member variables (constant variables, references, static) in C + + classes-additional content to be added

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.