Resources:
1. Huang Bongyong Handsome
2.http://blog.163.com/sunshine_linting/blog/static/448933232011810101848652/
3.http://www.cnblogs.com/zhoug2020/archive/2012/08/31/2665451.html
First, the list of initialization
1. If the class www is defined www::www (int i, int j, int k): x (i), Y (j), Z (k) {}, a constructor with i,j,k three parameters is defined
2. Initialize the list initialization order: the initialization order of the Initialize list is performed in the order in which member variables are declared in the class, rather than in the order in which the initialization list is listed.
For example, in class Hyong, declare int a,b,c, then Hyong (): C (a), B (2), a (3) {} statement execution order is first to initialize a to 3, then B to 2, and finally assign a value to variable C, which is correct.
But Hyong (): C (1), B (2), A (c) {} will go wrong, because the order of execution is to assign the value of variable c to variable a first, instead of assigning the integer 1 to the variable C, so the variable C is not initialized, and the variable a gets an incorrect value.
3. The case where the initialization list must be used: const member, reference member assignment
Second, special member variables
1. Static member variables
#include <iostream.h>classMyclass { Public: Myclass (intAintBintc); voidgetsum ();Private: inta,b,c;Static intSum;//To declare static data member declarations, add static}; intmyclass::sum=0;//defines and initializes static data member definitions when initialization is not staticMyclass::myclass (intAintBintc) { This->a=A; This->b=b; This->c=C; Sum+=a+b+C;} voidmyclass::getsum () {cout< <"sum="< <sum < <Endl;}
1. Static data members are stored in the global data area. Static data members are defined when they are allocated space, so they cannot be defined in a class declaration.
2. Static data members and ordinary data members comply with PUBLIC,PROTECTED,PRIVATE access rules
3. Because static data members allocate memory in the global data area, all objects of this class are shared, so it is not part of a particular class object, and its scope is visible when no class object is produced, that is, when there is no instance of the class, we can manipulate it
4. within the class is a declaration, outside the class is defined!
5. Static member functions
Static member functions, like static data members, are internal implementations of the class and are part of the class definition. Ordinary member functions generally imply a this pointer, which points to the object of the class itself, because ordinary member functions are always specific to the specific object of a class. Typically, this is the default. such as the function fn () is actually THIS->FN ().
However, a static member function, as opposed to a normal function, is not associated with any object, so it
does not have this pointer. In this sense, it
cannot access non-static data members that belong to a class object , nor can it access a non-static member function,
which only calls the rest of the static member functions .
#include <iostream.h>classMyclass { Public: Myclass (intAintBintc); Static voidGetsum ();declaring a static member functionPrivate: inta,b,c;Static intSum;//declaring a static data member}; intmyclass::sum=0;//defining and initializing static data membersMyclass::myclass (intAintBintc) { This->a=A; This->b=b; This->c=C; Sum+=a+b+c;//non-static member functions can access static data members} voidMyclass::getsum ()//implementation of static member functions{ //cout < <a < <endl;//error code, a static member function of a non-static data member because it is not associated with any object, so it does not have this pointer. In this sense, it cannot access non-static data members that belong to a class object, nor can it access a non-static member function, which only calls the rest of the static member functions. Cout < <"sum="< <sum < <Endl;} voidMain () {Myclass M (1,2,3); M.getsum (); Myclass N (4,5,6); N.getsum (); Myclass::getsum (); }
For static member functions, you can summarize the following points:
A function definition that appears outside the class body cannot specify a keyword static;
Static members can be accessed from one another, including static member functions that access static data members and access static member functions;
Static member functions and static data members can be accessed arbitrarily by non-static member functions;
Static member functions cannot access non-static member functions and non-static data members;
Because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;
2. Other special member variables and initialization
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.
① Simple const variable and reference variable: must be initialized in the initialization list. and the initialization list is used in all constructors .
② variables with static modifiers , whether ordinary or const, are initialized outside the class and are not in the constructor.
③static const int variable, which can be initialized in the declaration of a class.
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.
Special member variable + initialization list for the "C + +" class