The static in C + +
There are two uses for static in C/C + +: Static in process design and static in object-oriented programming. The former applies to ordinary variables and functions, and does not involve class problems.
A. Static keywords for process programming
1) Static global variables
Before the global variable, the variable is defined as a static global variable, plus the keyword static. Static global variables are defined and used similar to:
#include <iostream>
using namespace Std;
VOID Fn ();
static int n; Defining static global Variables
void Main ()
{
n=20;
cout<<n<<endl;
FN ();
}
VOID Fn ()
{
n++;
cout<<n<<endl;
}
Characteristics of static global variables:
A. Static variables (including static global variables and static local variables) allocate memory in the global data area;
B. Static variables, whether static local or static global variables are initialized only once.
C. Uninitialized static global variables are automatically initialized to 0 or spaces. (The value of the normal variable, which is the automatic variable, is random unless it is explicitly initialized)
D. A static global variable is visible in the entire file that declares it, and is not visible outside the file, that is, variables of the same name can be defined in other files, and no conflicts occur
2) Static local variables
Before a local variable, the variable is defined as a static local variable, plus the keyword static. Static local variables are defined and used similar to:
#include <iostream>
using namespace Std;
VOID Fn ();
void Main ()
{
FN ();
FN ();
FN ();
}
VOID Fn ()
{
Static n=10; Static local variables are defined, initialized only once!
cout << N <<endl;
n++;
}
Output Result:
10
11
12
Typically, a variable is defined in the body of the function that allocates stack memory to the local variable whenever the program runs to the statement. However, as the program exits the function body, the system will retract the stack memory, and the local variables are invalidated accordingly. But sometimes we need to save the values of the variables between the two calls. The usual idea is to define a global variable to implement. In this way, the variables are no longer part of the function itself, and are no longer only controlled by the functions, which inconvenience the maintenance of the program. Static local variables can solve this problem. Static local variables are saved in the global data area, not in the stack, and each time the value is persisted to the next call until the next time the new value is assigned.
Characteristics of static local variables:
A. The variable allocates memory in the global data area;
B. A static local variable is first initialized when the program executes to the declaration of the object, that is, the subsequent function call is no longer initialized;
C. Static local variables are usually initialized at the declaration, and if they are not explicitly initialized, the program is automatically initialized to 0 or spaces;
D. It always resides in the global data area until the program finishes running. But its scope is a local scope, and when the function or block of statements that defines it ends, its scope ends.
3) static function
With the static keyword preceded by the return type of the function, the function is defined as a static function. A static function differs from a normal function in that it can only be seen in the file that declares it and cannot be used by other files. The following is an example program that declares and uses static functions:
#include <iostream>
using namespace Std;
static void Fn (); declaring static functions
void Main ()
{
FN ();
}
VOID FN ()//define static functions
{
int n=10;
cout<<n<<endl;
}
B. Static keywords in object-oriented programming
All instructions for the static keyword in a are still applicable. The following is a discussion of the properties specific to object-oriented about the static keyword.
1) static member variables
Precede the declaration of a member variable within a class with the keyword static, which is the static member variable within the class. Give an example of a static member variable first:
#include <iostream>
using namespace Std;
Class Myclass
{
Public
Myclass (int a,int b,int c);
void Getsum ();
Private
int a,b,c;
static int Sum; declaring static member variables
};
int myclass::sum = 0; Defines and initializes a static member variable. A static member variable must be declared in a class, and the class is set out
Myclass::myclass (int a,int b,int c)//meaning, the static keyword can no longer be present when defined. Can be non-initialized, which can be changed to:
{//int myclass::sum;. This is sum is initialized to 0 by default because it is a static variable.
this->a=a;
this->b=b;
this->c=c;
sum+ = A+b+c;
}
void Myclass::getsum ()
{
cout<< "sum=" << Sum <<endl;
}
int main (void)
{
Myclass M (a);
M.getsum ();
Myclass N (4,5,6);
N.getsum ();
M.getsum ();
return 0;
}
As you can see, static data members have the following characteristics:
For non-static member variables, each class object has its own copy. Static member variables are treated as members of the class. Regardless of how many objects of this class are defined, static member variables have only one copy in the program and are shared by all objects of that type. In other words, a static member variable is common to all objects of that class. For multiple objects of this class, static member variables are allocated only once for all objects to be shared. Therefore, the value of a static member variable is the same for each object, and its value can be updated;
Static member variables are stored in the global data area. Static member variables are defined when you want to allocate space, so you cannot define them in a class declaration. In the above code, the statement int myclass::sum=0 is defined as a static member variable;
Static member variables conform to public,protected,private access rules as normal member variables;
Because static member variables allocate memory in the global data area, all objects of this class are shared, so it is not part of a particular class object, and its scope is visible when no class object is produced, that is, when there is no instance of the class, we can manipulate it;
Static member variable initialization differs from general member variable initialization. The static member variable is initialized in the following format:
< data type >< class name >::< static member variable name >=< value >
A static member variable of a class has two types of access:
< class object name >.< static member variable name > or < class object pointer >->< static member variable name > or < class type name >::< static member variable name >
If the access permission for a static member variable is allowed (that is, the member of public), the static member variable can be referenced in the program in the format described above;
Static member variables are primarily used when each object has the same property. For example, for a deposit class, the interest is the same for each instance. Therefore, the interest should be set to the static member variable of the deposit class. There are two benefits, first, regardless of how many deposit class objects are defined, the interest member variables share the memory allocated in the global data area, thus saving storage space. Second, once the interest needs to be changed, the interest of all the deposit classes will change as soon as it is changed;
There are two advantages to using static member variables compared to global variables:
Static member variables do not enter the program's global namespace, so there is no possibility of conflicts with other global names in the program;
Information hiding can be implemented. A static member variable can be a private member, while a global variable cannot.
2) static member function
As with static member variables, we can also create a static member function that serves the entire service of a class rather than just a specific object of a class. Static member functions, like static member variables, are internal implementations of the class and are part of the class definition. Ordinary member functions generally imply a this pointer, which points to the object of the class itself, because ordinary member functions are always specific to the specific object of a class. Typically, this is the default. such as the function fn () is actually THIS->FN (). However, compared to a normal function, a static member function does not have the this pointer because it is not associated with any object. In this sense, it cannot access a non-static member variable that belongs to a class object, and cannot access a non-static member function, it can only invoke the rest of the static member functions. Here is an example of a static member function.
#include <iostream>
using namespace Std;
Class Myclass
{
Public
Myclass (int, int, int);
static void Getsum (); Declaring a static member function
Private
int A;
int b;
int C;
static int sum; declaring static member variables
};
int myclass::sum = 0; Define and initialize static member variables, this must be true! See above for an explanation.
Myclass::myclass (int A, int b, int c)
{
This->a = A;
This->b = b;
This->c = C;
Sum + = (A + B + c); Static member variables can be accessed by non-static member variables
}
void Myclass::getsum ()//static member function implementation, note that there cannot be a static keyword here
{
cout << a << Endl; Error code, a non-static data member, static member function cannot access
cout << "sum =" << sum << Endl;
}
int main (void)
{
Myclass M (1, 2, 3);
M.getsum (); Access by < object >.< static member function name > (parameter table)
Myclass N (4, 5, 6);
N.getsum ();
Myclass::getsum (); Access by < class name >::< static member function name > (parameter)
myclass* PO = new Myclass (7, 8, 9);
Po->getsum (); Access with < object pointer >->< static member function name > (parameter table)
return 0;
}
For static member functions, you can summarize the following points:
① a function definition that appears outside the class body cannot specify the keyword static;
② static members can be accessed from one another, including static member functions accessing static data members and accessing static member functions;
③ static member functions and static data members can be accessed arbitrarily by non-static member functions;
④ static member functions cannot access non-static member functions and non-static data members;
⑤ because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;
⑥ calls a static member function, which can be accessed with the member access operator (.) and (a) call a static member function for an object of a class or pointer to a class object, or you can use the format: < class name >::< static member function name > (< parameter table >) to invoke the static member functions of the class.
The static in C + +