Class C ++ special member variables (reference, static, regular member variables) initialization operation
Some member variables have special data types and their initialization methods are different from those of common data types. These special types of member variables include:
A. Reference
B. Constants
C. Static
D. Static constants (integer)
E. Static constants (non-integer)
Constants and references must be initialized through the parameter list.
Static member variable Initialization is also quite special. It is initiated outside the class and cannot carry static keywords. Its essence is at the end of this article.
Refer to the following code and comment:
# Include
Using namespace std; class BClass {public: BClass (): I (1), ci (2), ri (I) {} // for constant-type member variables and referenced member variables, they must be initialized through the parameterized list. // common member variables can also be placed in the function body, however, in essence, it is not initialization, but a common operation --> value assignment, which is also inefficient. private: int I; // common member variable const int ci; // constant member variable int & ri; // reference member variable static int si; // static member variable/static int I2 = 100; // error: only static constant member variables can be initialized in this way static const int csi; // static constant member variable static const int csi2. // initialization of static constant member variables (Inte Gral type) (1) static const double csd; // static constants member variable (non-Integral type) // static const double csd2 = 99.9; // error: only static constant integer data members can be initialized in the class}; // note the following three rows: no static int BClass: si = 0; // The initialization of static member variables (Integral type) const int BClass: csi = 1; // The initialization of static member variables (Integral type) const double BClass :: csd = 99.9; // initialization of static constant member variables (non-Integral type) // when initializing CI2 in (1), according to the famous master Stanley B. this line is required in Lippman's statement. // But in VC2003, if there is one line below, an error will occur. In VC2005, the following line is dispensable, which is related to the compiler. Const int BClass: CI2; int main () {BClass B; return 0 ;}
Static members belong to the class scope, but do not belong to class objects. Like common static variables, the program allocates and initializes the memory as soon as it is run. The life cycle is the same as that of the program.
Therefore, it is unreasonable to initialize static variables in the class constructor.
Static members are actually in the same position as global variables, except that the compiler limits their usage to class scopes (not class objects, but not Class Object members ), it must be initialized outside the class definition (not outside the class scope.
The member variables are referenced as follows:
Because the reference is an alias, the application must be defined externally and then initialized in the constructor initialization list.
# Include
Using namespace std; class A {public: A (int I = 3): m_ I (I) {} void print () {cout <"m_ I =" <
In this way, the object a can exist in B,
[Note]: initialization of reference type member variables:
1. The initialization list cannot be directly initialized in the constructor, and the parameter must also be of the reference type.
2. No default constructor is allowed for classes with referenced member variables. The reason is that the member variables of the reference type must be initialized during class construction.
3. If two classes want to share data of the third class, consider using the third class as a member variable of the reference type of the two classes.