The static keyword is familiar to everyone, known as static. See how MSDN Describes static:
When modifying a variable, the static keyword specifies this variable has static duration (it is allocated when the PR Ogram begins and deallocated when the "program ends" and initializes it to 0 unless another of the value is specified. When modifying a variable or function in file scope, the static keyword specifies that variable or function has intern Al Linkage (its name isn't visible from outside, the file in which it is declared).
In C + +, when modifying a data member in a class declaration, the "static keyword specifies that one copy of" is S Hared by the instances of the class. When modifying A's function in a class declaration, the static keyword specifies this function accesses only stat IC members.
Google translation is as follows:
When modifying a variable, the static keyword specifies the duration of the variable's static (it is allocated and disposed at the end of the program), and is initialized to 0 if no other value is specified. When a variable or function is modified within a file scope, the static keyword specifies a variable or function internal connection (its name is not visible from outside the file that declares it).
In C + +, when a data member is modified in a class declaration, the Static keyword specifies that a copy of the member is shared by all instances of the class. To declare a member function in a class, the static keyword specifies that the function accesses only static members.
So today we're going to recognize the static keyword in two parts:
1 Process-oriented design (c + + all applicable)
2 Object Design (Class)
1.1 Static global variables
The variable is defined as a static global variable before the global variable, plus the keyword static. Let's first give an example of a static global variable, as follows:
#include <iostream>
using namespace std;
VOID Fn ();
static int n; Defines a static global variable
void main ()
{
n=20;
cout<<n<<endl;
fn ();
}
VOID fn ()
{
n++;
cout<<n<<endl;
}
Static global variables have the following characteristics:
• This variable allocates memory in the global data area;
• Uninitialized static global variables are automatically initialized by the program to 0 (the value of the automatic variable is random unless it is explicitly initialized);
• Static global variables are visible throughout the file in which they are declared, but not outside of the file;
But if we discard the static keyword in the appeal code.
#include <iostream>
using namespace std;
VOID Fn ();
int n; Defines the global variable
void main ()
{
n=20;
cout<<n<<endl;
fn ();
}
VOID fn ()
{
n++;
cout<<n<<endl;
}
The program still works. But what difference does it have?
• Static global variables cannot be used by other files;
• Other files can be defined with the same name of the variable, does not collide;
The static-decorated variable or function can only be seen in this file, and the const-decorated default is the internal link property, which is decorated with external link properties (and when const is decorated with the const modifier).
1.2. Static local Variables
Before a local variable, plus the keyword static, the variable is defined as a static local variable. Let's first give an example of a static local variable, as follows:
#include <iostream>
using namespace std;
VOID Fn ();
void Main ()
{
fn ();
fn ();
fn ();
}
VOID fn ()
{
static n=10;
cout<<n<<endl;
n++;
}
Typically, a variable is defined in the body of the function that allocates stack memory to the local variable whenever the program runs to that statement. However, as the program exits the function body, the system will reclaim the stack memory and the local variables should be invalidated accordingly. But sometimes we need to save the value of a variable between two calls. The usual idea is to define a global variable to implement. But in this way, the variable is no longer the function itself, is no longer only controlled by the function, to the maintenance of the program inconvenience.
Static local variables can just solve the problem. Static local variables are saved in the global data area, not on the stack, and the value is persisted to the next call until the next time the new value is assigned.
Static local variables have the following characteristics:
• This variable allocates memory in the global data area;
• Static local variables are initialized for the first time when the program executes to the declaration of the object, that is, subsequent function calls are no longer initialized;
• Static local variables are typically initialized at the declaration and, if not explicitly initialized, are automatically initialized to 0 by the program;
• It always resides in the global data area until the program has finished running. But its scope is a local scope, and when the function or statement block that defines it ends, its scope ends;
1.3 Static Functions
By adding the static keyword before the return type of the function, the function is defined as a static function. A static function, unlike a normal function, can only be visible in the file in which it is declared and cannot be used by other files.
Examples of static functions:
#include <iostream>
using namespace std;
static void Fn ();//Declaring a quiescent function
void Main ()
{
fn ();
}
VOID FN ()//defines a static function
{
int n=10;
cout<<n<<endl;
}
The benefits of defining static functions:
• Static functions cannot be used by other files;
• Functions with the same name can be defined in other files, and no conflict occurs;
2.1 Static data members
Add a keyword static before the declaration of a data member within a class, which is a static data member within a class. Give an example of a static data member first.
#include <iostream> using namespace std; class Myclass {public:myclass (int a,int b,in
T c);
void Getsum ();
Private:int A,b,c;
The static int sum;//declares statically data members};
int myclass::sum=0;//defines and initializes a static data member myclass::myclass (int a,int b,int c) {this->a=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 (); }//Run Result: sum=6 sum=21 sum=21
You can see that static data members have the following characteristics:
• For non-static data members, each class object has its own copy. Static data members are treated as members of a class. No matter how many the objects of this class are defined, static data members have only one copy in the program , and access is shared by all objects of that type. In other words, static data members are common to all objects of the class. For multiple objects of the class, static data members are allocated only once in memory for all objects to share. Therefore, the value of the static data member is the same for each object, and its value can be updated;
• Static data members are stored in the global data area. Static data member definitions are allocated space, so they cannot be defined in class declarations. in statement int myclass::sum=0; is the definition of static data members;
• Static data members conform to public,protected,private access rules like regular data members;
• Because static data members allocate memory in the global data area, All objects belonging to this class are shared, so it is not part of a particular class object and its scope is visible when no class object is generated, that is, we can manipulate it without producing an instance of the class;
• Static data member initialization differs from general data member initialization. The format for static data member initialization is:
The data type >< class name >::< static data member name >=< value >
The static data member of the class has two forms of access:
Class object name >.< static data member name > or class type name >::< static data member name >
if the static If the access permission of the data member is allowed (that is, members of public), the static data members can be referenced in the program in the format described above;
• Static data members are used primarily when each object has the same attribute. For example, for a deposit class, the interest for each instance is the same. Therefore, interest should be set as a static data member of the deposit class. There are two benefits, first, that the interest data members share the memory allocated in the global data area, regardless of the number of deposit objects defined, thus saving storage space. Second, once the interest needs to change, as long as the change, the interest of all the deposit classes have changed;
• There are two advantages to using static data members compared to global variables:
1. Static data members do not have access to the global namespace of the program, Therefore, there is no possibility of conflict with other global names in the program;
2. You can implement information hiding. Static data members can be private members, and global variables cannot;
2.2 Static member functions
As with static data members, we can also create a static member function that serves all of the class's services rather than the specific object of a particular class. Static member functions, like static data members, are internal implementations of a class and are part of the class definition. Ordinary member functions implicitly imply a this pointer, the this pointer points to the object itself of the class, because the ordinary member function is always specific to the specific object of a class. Typically, this is the default. such as the function fn () is actually THIS->FN (). However, a static member function is without this pointer because it is not associated with any object, as opposed to a normal function. In this sense, it does not have access to non-static data members belonging to the class object, nor does it have access to non-static member functions, and it can only invoke the rest of the static member functions. Here's an example of a static member function.
#include <iostream>
using namespace std;
Class Myclass
{public
:
Myclass (int a,int b,int c);
static void Getsum ();/declaring a statically member function
private:
int a,b,c;
The static int sum;//declares statically data members
};
int myclass::sum=0;//defines and initializes a static data member
myclass::myclass (int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
Sum+=a+b+c; Non-static member functions can access static data member
}
void Myclass::getsum ()///static member function implementation
{/
/cout<<a<<endl;// Error code, 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 ();
}
For static member functions, you can summarize the following points:
• A function definition that appears outside the class body cannot specify a keyword static;
• Static members can access each other, including static member functions to access static data members and access to static member functions;
• Non-static member functions can access static member functions and static data members arbitrarily;
• Static member functions cannot access non-static member functions and non-static data members;
• Because there is no extra overhead for this pointer, static member functions have a slight increase in speed compared to the global functions of a class;
• Call static member functions, which can be accessed using the member access operator (.) and (->) calls a static member function for an object of a class or a pointer to a class object.