From the question, Here we only discuss the static function in the class (static member of the class), that is, two points: static data member and static member function.
Static MemberProposal: To solve the data sharing problem.
Of course, global objects can also achieve data sharing, but static has the followingTwo advantages:
(1)Encapsulation: static members can be private members, while global objects are global. Generally, this value can be modified in user code.
(2)Avoid Name Conflict: The static member name is in the scope of the class, so it can avoid conflicts with other class members or global object names.
Note: Static members belong to the class and are irrelevant to the class objects.
I. static data member
Static refers to the lifetime of modified variables. Static survival period refers to the data stored in the "Global Data zone". Once a program runs, it begins to exist and the program ends, it is automatically released by the system.
Definition (initialization):
The data members of a class cannot be initialized in the declared body of the class. The static member initialization (in vitro) form is:
<Data type><Class Name> ::<Static data member name>=<Value> ;//Here (when defined) does not add static (required during internal class declaration) to avoid confusion with common static variables or objects.
Statement:
In the class body, you must declare:
Static <data type> <static data member name>;
Note: Because the class declaration only declares the "size and specification" of a class and does not actually allocate memory. Therefore, writing a definition in the class declaration is incorrect.
Reference:
<Class name >:: <static data member name>
For example:
Class widget {public: static int var; // declare...}; int Widget: Var = 10; // initialization upon definition
Special integer const static member:
As long as the initialization is a constant expression, the members of the integer const static data must be initialized in the class definition body (Declare Initialization). In this case, in the definition of the class, the member must also be defined (no need to specify the initial value ).
For example:
Class widget {public: static const int con_var = 10; // yes, right. Here is the declaration...}; const int widget: con_var; // Definition
Ii. static member functions
Because the static class has nothing to do with the object, the static member function does not include the this pointer, that is, the member variable of the object cannot be operated (that is, the object individual is not recognized ).
Therefore, the static member function is mainly used to operate static data members (which also belongs to classes ).
In addition to the difference between static and common member functionsTwo differences:
(1)Static member function addresses can be used to store common function pointers, while common member function addresses must be stored using class member function pointers.
For example:
Class widget {public: static int funA (); int funB () ;}; int (* pfA) () = & widget: funA; // normal function pointer int (base: * pfB) () = & base: funB; // member function pointer
(2)Static member functions cannot be declared as virtual, const, or volatile at the same time.
For example:
class widget{pubilc:virtual static void funA(); //Errorstatic void funB() const; //Errorstatic void funC() volatile; //Error...};
References:
1. http://www.yesky.com/20010828/194000.shtml
2. http://blog.csdn.net/danky/article/details/1447011
3. C ++ Primer