Recently I've been looking at C + + related projects, but it's always a blur to understand static member variables and static member functions in C + + classes, and I don't understand why static member variables are used in a class. So I collected some information on the Internet, and then summed it up a little.
The concept of static members:
a member in a static class joins the static modifier, which is a statically member. You can access this static member directly by using the class name + static member name, because a static member exists in memory before the declaration of the class, or it can be accessed based on the object declared by the class. Instead of allocating memory, the non-static member must be instantiated.
The concept of non-static members:
all non-static members are nonstatic. When a class is instantiated, it can be accessed by instantiating the class name. The lifetime of a non-static member is determined by the lifetime of the class. The static member does not have a production period problem because it always resides in memory.
It is summed up in two aspects, process-oriented and object-oriented.
One: The static keyword for the process
1. Static Global variables
Before defining a global variable, plus the keyword static, the variable is defined as a static global variable.
Characteristics:
- This variable allocates memory in the global data area.
- Initialize: If the initialization is not displayed, it is implicitly initialized to 0.
- The visit variable is only visible in this document, that is, the definition should start at the end of this document.
Procedures are generally divided into four regions:
- Code Area
- Global Data area
- Heap Area
- Stack area
The general program is placed in the heap by the dynamic data generated by new, and the automatic variables inside the function are placed in the stack area. Automatic variables typically free up space as the function exits, and static data (even static local variables inside the function) is stored in the global data area. Therefore, they do not release space as the function exits.
static int n;//defines a statically global variable
instead: int n;//define global variables
Difference:
static global variables cannot be used by other files. Thus other files can define variables of the same name without conflicts.
Example:
//Example//File 1#include <iostream>voidfn ();Static intN//define static global variables (only available in this file)voidMain () {n= -; cout<<n<<Endl; fn ();}//File 2#include <iostream>extern intN; (this variable can be referenced in another file)voidfn () {n++; cout<<n<<Endl;}//compile and run example, and you will see that the above code is compiled separately, but the link will go wrong.if the staticintN instead:intN; Compile and run the program again, there will be no error.
2. Static local Variables
Static local variables are defined by adding the static keyword before the local variable.
Characteristics:
A. This variable allocates memory in the global data area.
B. When initializing: If the initialization is not displayed, it is implicitly initialized to 0.
C. It always resides in the global data area until the end of the program. But its scope is local scope. When you define its function or statement block, its scope ends.
3. Static function: Add the static keyword before the return type of the function.
Features: Static functions, unlike normal functions, can only be seen in the file that declares it and cannot be used by other files.
Two: Object-oriented static keywords
1. Static data members
The declaration of a data member in a class is preceded by static, which is a statically data member of the class.
Example:
//Example 2#include <iostream>classmyclass{ Public: MyClass (intAintBintc); voidgetsum ();Private: intA,b,c; Static intSum//declaring a static data member};intmyclass::sum=0;//defining and initializing static data members
Characteristics:
For non-static data members, each class object has its own copy. While static data members are treated as members of a class, no matter how many of the classes are defined, static data members have only one copy, which is shared for all objects of that type (including their derived classes). So, the value of the static data member is the same for each object, Its value can be updated.
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 can be used without producing a class object.
2. Static member functions
A static member function does not have the this pointer, as compared to a normal member function, because it is not associated with any object. In this sense, it cannot access non-static data members that belong to a class object, cannot access non-static member functions, and can only invoke other static member functions.
Static member variables and static member functions in C + + classes