Data members can be divided into static variables and non-static variables.
Static member: A member in a static class is added with a static modifier, which is a static member. you can directly use the class name + static member name to access this static member. Because the static member exists in the memory, non-static members need to be instantiated to allocate the memory, so static members cannot access non-static members .. because static members exist in the memory, non-static members can directly access static members in the class.
Non-static member: all non-static members are non-static members. After the class is instantiated, it can be accessed by the instantiated class name .. the lifetime of a non-static member is determined by the lifetime of the class .. static members do not have the concept of survival, because static members always reside in the content ..
A class can also contain static and non-static members. The class also includes static constructors and non-static constructors ..
It is summarized in two aspects: the first is relative to the process-oriented, that is, the class is not involved in this aspect, and the second is relative to the object-oriented, it mainly describes the role of static in the class.
I. Static keywords in process-oriented design
1. Static global variables
Definition: Before a global variable, add the keyword "static". The variable is defined as a static global variable.
Features:
A. The variable allocates memory in the global data zone.
B. Initialization: If Explicit initialization is not performed, the value 0 is implicitly initialized (automatic variables are random unless explicitly initialized ).
C. The access variable is only visible in the source file. Strictly speaking, it should be defined from the beginning to the end of this file.
Example (from the C ++ Programming Tutorial --- Qian Neng, editor of p103): // file1.cpp
// Example 1
# Include <iostream. h>
Void FN ();
Static int N; // defines the static global variable.
Void main ()
{
N = 20;
Cout <n <Endl;
FN ();
}
Void FN ()
{
N ++;
Cout <n <Endl;
}
D. The constant declared in the file scope is static storage by default.
All static variables are allocated memory in the global data zone, including the static local variables to be mentioned later. For a complete program, the distribution in the memory is as follows:
Code Area
Global data Zone
Heap Area
Stack Zone
In general, dynamic data generated by new is stored in the heap zone, and automatic variables in the function are stored in the stack zone. Automatic variables usually release space as the function exits, and static data (even static local variables in the function) is stored in the global data zone. The data in the global data zone does not release space because the function exits. Careful readers may find that
Static int N; // defines the static global variable.
Changed:
Int N; // The program that defines global variables still runs normally. Indeed, defining global variables can share variables in files, but defining static global variables has the following benefits:
Static global variables cannot be used by other files; (it seems to be different from extern)
Variables with the same name can be defined in other files without conflict;
You can change the Sample Code as follows:
// Example 2
// File1
# Include <iostream. h>
Void FN ();
Static int N; // defines static global variables (only available in this file)
Void main ()
{
N = 20;
Cout <n <Endl;
FN ();
}
// File2
# Include <iostream. h>
Extern int N; (this variable can be referenced in other files)
Void FN ()
{
N ++;
Cout <n <Endl;
} Compile and run example 2, and you will find that the above Code can be compiled separately, but an error occurs during link. Try to define static int N; // static global variable
Change
Int N; // defines the global variable
Compile and run the program again to understand the differences between global variables and static global variables. 2. Static local variable definition: When the static keyword is added before the local variable, the static local variable is defined. Let's take an example of a static local variable as follows: // Example 3
# Include <iostream. h>
Void FN ();
Void main ()
{
FN ();
FN ();
FN ();
}
Void FN ()
{
Static n = 10;
Cout <n <Endl;
N ++;
}
Generally, a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid.
But sometimes we need to save the variable value between two calls. The general idea is to define a global variable for implementation. In this way, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.
Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.
Features:
A. The variable allocates memory in the global data zone.
B. Initialization: If Explicit initialization is not performed, the value 0 will be implicitly initialized, and subsequent function calls will not be initialized.
C. It always resides in the global data zone until the program is running. But its scope is local scope. When the function or statement block defining it ends, its scope ends.
Summary: in fact, there are two attributes in the program, scope and life cycle. (Lifecycle refers to the time when a variable appears to die out, and scope refers to the time when the variable is valid within a range ). Then there are two sets of attributes for variables: Long Life Cycle (same as the life cycle of the Program), short life cycle (temporarily generated and then temporarily extinct )], scope [Global Action (entire program), local action ]. Obviously, the two attributes in a set are mutually exclusive, SO 2*2 = 4 according to the combination of the two attribute sets, in C language (or process-oriented programming), there is a combination of the two attributes, that is to say, the location of the defined variable [in all external definitions of the function, in the internal definition of the function], and the static keyword [used in different places, it produces two results ]. Well, let's talk about these four combinations, ① a combination of local scopes and short life periods. (In C language, variables are defined inside the function (without the static keyword) ② a combination of local scopes and long life periods. (In C language, variables are defined inside the function (with the static keyword). [these two mainly look at the issue of life cycle, the role of static is obviously to expand the life cycle of variables. ③ global and long life periods (in C language, they are defined outside all functions without static) ④ global scope and short life cycle (this short life cycle is relative to the variables that are valid for the entire program). (This is manifested in the external definition of all functions in the C language, using static
Keyword) [In fact, global variables are stored in independent memory units. When the static keyword is not added, they are valid throughout the program. However, after the static keyword is added, the scope of the variable is limited to the file that defines the variable. Obviously, the static keyword is used to limit the scope of the global variable]
There are two types of variables from the storage location: Fixed memory units (global) and stack.
3. Static functions (note the differences between them and static member functions of the class): a function is defined as a static function by adding the static keyword before the return type of the function. Features:
A. Static functions are different from common functions. They can only be seen in the files declared for them and cannot be used by other files. Example of static functions: // Example 4
# Include <iostream. h>
Static void FN (); // declare a static function
Void main ()
{
FN ();
}
Void FN () // defines static functions
{
Int n = 10;
Cout <n <Endl;
} Benefit of defining static functions: static functions cannot be used by other files;
Functions with the same name can be defined in other files without conflict;
2. Object-oriented static keywords (static keywords in the class)
1. static data member
Add the keyword static before the declaration of the data member in the class. The data member is the static data member in the class. Here is an example of a static data member.
// Example 5
# Include <iostream. h>
Class myclass
{
Public:
Myclass (int A, int B, int C );
Void getsum ();
PRIVATE:
Int A, B, C;
Static int sum; // declare a static data member
};
Int myclass: Sum = 0; // defines and initializes static data members.
Myclass: myclass (int A, int B, int C)
{
This-> A =;
This-> B = B;
This-> C = C;
Sum + = A + B + C;
}
Void myclass: getsum ()
{
Cout <"sum =" <sum <Endl;
}
Void main ()
{
Myclass M (1, 2, 3 );
M. getsum ();
Myclass N (4,5, 6 );
N. getsum ();
M. getsum ();
} It can be seen that static data members have the following features:
For non-static data members, each class object has its own copy. Static data members are treated as class members. No matter how many objects are defined for this class, static data members also have only one copy in the program, which is shared by all objects of this type. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects to share. Therefore, the values of static data members are the same for each object, and their values can be updated;
Static data members are stored in the global data zone. Space must be allocated when defining static data members, so they cannot be defined in the class declaration. In Example 5, the statement int myclass: Sum = 0 is a member that defines static data;
Static data members follow the public, protected, and private access rules like common data members;
Because static data Members allocate memory in the global data zone and all objects in this class are shared, they do not belong to a specific class object and are visible in the scope when no class object is generated, that is, when no class instance is generated, we can operate on it;
Static data member Initialization is different from general data member initialization. The static data member initialization format is:
<Data type> <class name >:< static data member name >=< value>
Static data members of a class can be accessed in two forms:
<Class Object Name>. <static data member name> or <class type name >:< static data member name>
If the access permission of the static data member is allowed (that is, the Public Member), you can reference the static data member in the program in the above format;
Static data members are mainly used when each object has the same attribute. For example, for a deposit type, the interest of each instance is the same. Therefore, the interest should be set as the static data member of the deposit class. There are two advantages: first, no matter how many deposit-type objects are defined, interest data members share the memory allocated to the global data area, thus saving storage space. Second, once the interest needs to be changed, the interest of all deposit objects will be changed once;
Compared with global variables, static data members have two advantages:
The static data member does not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program;
Information can be hidden. Static data members can be private members, but global variables cannot;
2. static member functions
Like static data members, we can also create a static member function that serves all the classes rather than specific objects of a class. Static member functions, like static data members, are internal implementation of the class and are part of the class definition. A common member function generally implies a this pointer, which points to the object itself of the class, because a common member function always belongs to a specific object of a class. Generally, this is the default value. For example, the function FN () is actually this-> FN (). However, compared with common functions, static member functions do not have the this pointer because they are not associated with any objects. In this sense, it cannot access non-static data members of class objects or non-static member functions. It can only call other static member functions. The following is an example of a static member function.
// Example 6
# Include <iostream. h>
Class myclass
{
Public:
Myclass (int A, int B, int C );
Static void getsum ();/declare a static member function
PRIVATE:
Int A, B, C;
Static int sum; // declare a static data member
};
Int myclass: Sum = 0; // defines and initializes static data members.
Myclass: myclass (int A, int B, int C)
{
This-> A =;
This-> B = B;
This-> C = C;
Sum + = A + B + C; // non-static member functions can access static data members.
}
Void myclass: getsum () // static member function implementation
{
// Cout <A <Endl; // error code. A is a non-static data member.
Cout <"sum =" <sum <Endl;
}
Void main ()
{
Myclass M (1, 2, 3 );
M. getsum ();
Myclass N (4,5, 6 );
N. getsum ();
Myclass: getsum ();
}
Static member functions can be summarized as follows:
The keyword static cannot be specified for function definitions that appear in external classes;
Static members can access each other, including static member functions accessing static data members and accessing static member functions;
Non-static member functions can access static member functions and static data members at will;
Static member functions cannot access non-static member functions and non-static data members;
Without the additional overhead of this pointer, static member functions increase slightly compared with global functions of the class;
Call static member functions. You can use the member access operators (.) and (->) to call static member functions for a class object or a pointer to a class object.
Summary: in C language, a variable has three scopes: Global scopes, internal scopes of functions, and scopes in two braces. For functions, the entire program is valid. In this case, the C language provides a static mechanism to logically distinguish some scope problems.
For the C ++ language, we have a more detailed scope distinction, in addition to those in the original C language, there is also a class, class is the abstraction of an object (including the attributes of an object and the methods for modifying attributes ). With this mechanism, you can logically differentiate the function scope, which is within the class. But in fact, the function scope is the whole program. In addition, the namespace is used to logically differentiate these functions at a level.