How does static work?
In the interview, it is easy to ask the basic questions, but also the most easily ignored by everyone.
member variables of the static modifier class
A variable appears in class, such as:
Class Example
{
Public
static int A;
...
};
When a member variable is static, the static member variable belongs to the entire class, not to an object. The location of the storage, but also with ordinary variables have a difference:
Ordinary member variables, stored in the stack, each instantiated object's member variables, save their own data separately.
The static member variable stores only one copy of the RW segment that is placed in the data segment
Declaration of the static member variable:
Static data type variable name; Inside the class
Initialization of static member variables:
Data type class Name:: static data member = initial value; Static member variables must be initialized! Initialize outside of class
Second, static modifier member function
Static member functions, such as:
Class Example
{
Public
static int A;
Public
static int changeavalue (int value);
};
The format of the member function of the static decorated class:
Static member function name ();
The meaning of the static member function is not in information sharing, but in managing the static member variable and completing the encapsulation of the static member variable;
The static member function can access only data members at rest. Cause: A non-static member function that is called when the This pointer (the this pointer points to the current instance itself) is passed in as a parameter, whereas a static member function belongs to a class, not an object.
There is no this pointer.
What does the C + + learning---static do?