Static Data Member
When the data member declaration in the class is static, all instances of the class share the same Member, which is similar to the static variables in C functions. Static Data Generation
The Member is allocated to a fixed memory area during connection, just like a global variable. However, this identifier is only used when connected to the class name and scope identifier.
Is in the scope.
Data members generally use the same storage class to allocate memory. If an image is declared as auto, all its data is of the auto type, and static objects have static
Status data member. Static data members are an exception: when an object with static members is generated, static members do not allocate memory, because
Multiple Static versions are displayed. When declaring or initializing static variables, the representation is very similar to the Global object, for example:
Example 1 class with static data members
Class Example
{
Public:
Static int value; // declare a static member
Int ID;
};
Int example: Value // defines a static member
Void main ()
{
Example object1; // defines some objects.
Example object2;
Object1.value = 1; // use static variables
Object2.value = 10;
}
After a data member is declared as static, the connector only allocates memory for the member once and only after the member variable is defined. You can also
Initialize static.
// Define and initialize static data members
Int Example: value = 3;
The access permissions of static data members are different from those of non-static data members. No matter whether the data member is private, protected, or public, the function is always available.
Domain delimiters access static data members. For Example, in the above Example, even if the value is declared private or protected in the class Example, the declaration and initial
The Example: value statement is also valid. The reason is that the static data member accessed in this way is actually global data.
Using static members may cause unpredictable side effects. The two value assignment statements in main () use the same memory address, so that the second statement will overwrite
The first statement. It may not be obvious that you only check the code in main (). You need to check the Example Declaration for judgment. Storage
There are three methods for retrieving static data members:
1. Use the operator. In the preceding example, the compiler does not need to evaluate the expression on the left of. Because it is only defined for the accessed static member
A storage address, the normal access permission is also applicable.
2. Use the operator->. The pointer to the object is displayed on the left. Similarly, the compiler does not need to evaluate the-> left expression, but the normal class is
The member access permission is applicable.
3.
Use a Class Identifier instead of a specific object. This method does not strictly access all class data members, such as private, protected, and public.
. By using this method, static members can access:
// This is only a value assignment statement, not a storage definition.
Example: value = 1;
The code above also clearly states that the stored members are static members, because this notation is only used for static members.
Static variables are bridges connecting different instances in the same class. If a static variable is defined, the connector is
Memory allocation. One of the usage of static variables is to calculate the number of instances of a class.
Class Counter
{
Static long count;
Public:
Counter () {count ++ ;}
Long GetinstanceCount () {return count ;}
~ Counter () {count --;}
};
// Define and initialize static data members
Long Counter: count = 0;
Each time an object is generated, the static member count is increased by 1. To ensure the correctness of the result, initialize the static variable before the first variable is generated.
Note: static data members of a class can be defined, initialized, and used even if the class does not generate any instances.