First, Static Keyword 1, Static Global Variables in the Process Design
Definition: Before global variables, add keyword static that variable is defined as a static global variable.
Features:
A. This variable allocates memory in the global data area.
B. Initialization: If it is not explicitly initialized, it will be implicit to 0.
C. The visitor can only be seen in the present source file, and strictly should start to the definition to the end of this file.
Example (disappeared in C programming tutorial --- Money editor P103):
//file1.cpp
#include
Void fn ();
EXTERN INT N;
void main ()
{
N = 20;
Cout << n << endl;
Fn ();
}
//file2.cpp
#include
Static int N; // Define static global variables, initialization is 0;
Void fn ()
{
N ;
Cout << n << endl;
}
The files are compiled, but the variable n in file1.cpp is not defined, generating connection errors.
D, constant default default defaults to the file scope declared as a STATIC storage type.
2, static local variable
Definition: When a static keyword is added to the local variable, the static local variable is defined.
Features:
A. This variable allocates memory in the global data area.
B. Initialization: If it is not explicitly initialized, it will be implicit to 0.
C, it always resides in the global data area until the program runs. However, its scope is a local scope, and when defining its function or the end of the statement, its scope ends.
3, static function (note that the static member function of the class)
Definition: Plus static keywords before the return type of the function, the function is defined as a static function.
Features:
A. Static functions can only be used in this source file (this is the difference with ordinary function)
Example (disappeared in C programming tutorial --- Money editor P103):
//file1.cpp
Void fn ();
void staticfn ()
void main ()
{
Fn ();
staticfn ();
}
//file2.cpp
#include
Static void staticfn ();
Void fn ();
Void fn ()
{
staticfn ();
Cout << "this is fn ()
"
}
void staticfn ()
{
Cout << "this is staticfn ()
"
} When the connection is connected, an error cannot be found in the function staticfn ().
B, ideas
The inline function declared by the file scope is default to the static type.
Second, Static Keywords in the Object Object (mainly specified in the Static Keyword)
1, static data member
Features:
A, memory allocation: allocation in the global data area of the program.
B, initialization and definition:
A. Static data members are defined to assign space, so they cannot be defined in class declarations.
b, in order to avoid repeating the definition, where you use this class, you cannot be repeated, and you cannot be in the header file of the class.
definition.
c, static data members must exist because the programs are running, so the optimal location of its initialization is implemented internally in the class.
C, characteristics
a, the same as the public, protected, private keyword, like a normal data member, B, because its space is allocated in the global data area, which belongs to all types of object sharing, so it does not belong to a specific class object, It can be seen when the scope is not generated when there is no class object, ie when there is no instance of the class, we can operate it.
D, access form
a, class object name. Static data member name
B, class type name :: Static data member name
E, static data members, mainly used on all instances of the class. For example, for a deposit class, the account is different from each instance, but the interest per instance is the same. Therefore, interest should be set to static data members of the deposit class. This has two benefits, first, regardless of how many deposit class objects defined, interest data members share the memory allocated in the global zone, so save storage space. Second, once the interest needs to change, as long as it changes once, all the interests of all deposit class objects have changed, because they actually share something.
2, static member functions
Features:
A, static member functions are associated with class, not contact with the objects of the class.
B. Static member functions cannot access non-static data members. The reason is very simple, non-static data members belong to a specific class instance.
effect:
Mainly used for the operation of static data members.
Call form:
A, class object name. Static member function name ()
B, class type name :: Static member function name ()
Http://blog.blogchina.com/Article_20879.111948.html