In C, there is a static data type. Declaring a static data type means that the life cycle of the variable is
Static, that is, it is allocated to the program at the beginning of the program and released only when it is terminated. In C ++, declaring a static data member of a class means that all instances only have one copy of the data member.
The following is an explanation of MFC:
A static member is not part of an object but a part of a class. So the program can
Object. But you must initialize it first.
Do not schedule initialization of static member variables in the class constructor, because the constructor may be
Called again, and the initial value of the variable should be set only once. Do not arrange initialization operations in the header file,
Because it may be loaded to many places, it may be executed many times. You should be in the application file,
Class. For example, in Main, global function, or any function
Besides:
Double savingaccount: m_rate = 0.0075; // you can specify the initial value of the atatic member variable.
Void main (){... ...}
In this case, was m_rate a private data? It doesn't matter,Set initial values of static member variables
Is not subject to any access permissions.Note that the type of static member variables also appears in the initial value setting.
Because this is an initial value setting operation, not an assignment operation. In fact, Staic
Variables are defined at this time (not in the class Declaration. If you have not performed this initialization operation,
A connection error occurs.
The following is a way to access static member variables. Note that there are no object instances yet:
// The first access method:
Void main ()
{
Savingaccount: m_rate = 0.0075; // to set this parameter, change m_rate to public.
}
In this case, an object is generated and the static member variable is processed through the object:
// Method 2:
Void main ()
{
Savingaccount myaccount;
Myaccount. m_rate = 0.0075; // to set this parameter, change m_rate to public.
}
Static member variables do not exist because of the implementation of objects. They already exist. You can imagine
It is a global variable. As long as the access level permits, any function can access static member changes.
Quantity. However, if you want to access the private static member of the class before generating any object
Variable, you must design a static member function:
Because the static member function can be called without any object, the compiler does not
Add a this pointer to the blacklist. Therefore, the static member function cannot process non-static member variables in the class.