First, static data members
C + + allows you to define a data member of a class as a static member. A static data member belongs to a class, and the entire class has only one backup, which is equivalent to the global variables of the class and can be shared by all objects of that class.
1. Declarations of static members
The member is specified as a static data member by adding the keyword static before the declaration of the class data member. Static data members adhere to the qualifying rules for public, private, and protected access rights. Such as:
1 class Account2 {3 Public:4Account ();//constructor Function5~account ();// Destructors6 //.....................7 Private:8 Static intNumber//static data member declaration9 //.....................Ten}
2. Definition of static data members
Specifying a data member as a static member in the declaration of a class is only a declaration and does not allocate memory space for that member, which should be defined before use. Static data members are often defined outside the class, as is the method for defining a class member function. Define the form such as:
1 // type class name:: static member name; 2 // Type class Name:: static member name = initial value 3 int Account::number; // defines a static member and initializes it to 0
Note: (1) You cannot add a static qualifier when you define a data member outside of a class
(2) When you define a static data member, you can specify his initial value, and if the definition does not specify an initial value, the system will automatically assign the default value
3. Access to static data members
Static members belong to the entire class, and if you define him as a public member of a class, there are two ways to access it outside of the class.
A) Access by class name
Class Name:: Static member name
b) Access by object
The name of the object. Static member name
4. The difference between a static data member and a non-static data member
(1) Non-static data members are associated with objects, and each object has a separate backup of the data members. A static data member is associated with a class, and the entire class is backed up by a static data member, shared by all objects of the class. (c + + allocates independent memory space for non-static data members of an object, while static data members are allocated only once in memory)
(2) Non-static data members exist only after the object has been defined (that is, allocating memory space). A static data member does not belong to a single object, even if there is no object defining any of the classes he belongs to.
(3) The scope of a non-static data member is scoped to the block scope of the defined object, and the static data member is valid from the beginning of his definition to the end of the program.
Second, static member functions
By adding static to the prototype of a class member function, you define it as a static member function. A static member function belongs to the entire class, and he can access only static members of the class, including static data members and static member functions, and cannot access non-static members (including non-static data members and non-static member functions).
1. Declaration definitions for static member functions
1 class Account2 {3 Public:4Account ();//constructor Function5~account ();// Destructors6 //.....................7 Static intGetNumber ()//defining static member functions8 {9 returnNumber//accessing static data membersTen } One A Private: - Static intNumber//static data member declaration - //..................... the}
2. Invocation mode
There are two ways to call a static member function:
A) class invocation mode
Class Name:: Static member function name (parameter table);
B) Object invocation
The name of the object. Static member function name (parameter table);
3. Special attention
(1) Like normal member functions, static member functions can also be defined within a class or outside the class, and can also be defined as inline functions
(2) Static functions can only access static members (including static data members and static member functions) and cannot access non-static members (including non-static data members and non-static member functions)
(3) A static qualifier cannot be added when defining a statically member function outside of a class
(4) A static member function can be called before any object of the class is defined, and non-static members are accessible only after the object is defined
PS: If there is any wrong place, I hope that the predecessors peer can criticize that I will be grateful!!!!
Static members of C + + learning