Before proceeding to this Chapter, if we want to share a certain data within a certain range, we will set up a global object, but the object-oriented program is composed of objects, how can we share data within the scope of the class?
This is the focus of this chapter:
The declared static class members or member functions can be shared within the scope of the class. We will regard such members as static members and static member functions.
Below we use several instances to illustrate this problem. The class members need to be protected. Generally, in order not to violate the class encapsulation feature, we set the class member to protected (protection status) but in order to simplify the code and make the problem to be explained more intuitive and easy to understand, we set it to public here.
The following program is used as an example to simulate access. In the program, each time an object is created, the static class member we set will automatically add one. The Code is as follows:
# Include <iostream>
Using namespace STD;
Class Internet
{
Public:
Internet (char * Name, char * address)
{
Strcpy (Internet: Name, name );
Strcpy (Internet: Address, address );
Count ++;
}
Static void Internet: SC () // static member function
{
Cout <count <Endl;
}
Internet & RQ ();
Public:
Char name [20];
Char Address [20];
Static int count; // if it is written as static int COUNT = 0, it is incorrect.
};
Internet & Internet: RQ () // return the referenced member function
{
Return * this;
}
Int Internet: Count = 0; // initialization of static members
Void vist ()
{
Internet A1 (\ "China Software Development Lab \", \ "www.cndev-lab.com \");
Internet A2 (\ "China Software Development Lab \", \ "www.cndev-lab.com \");
}
Void FN (Internet & S)
{
Cout <s. Rq (). count;
}
Void main ()
{
Cout <Internet: count <endl; // output of static member values
Vist ();
Internet: SC (); // call a static member function
Internet B (\ "Chinese Software \", \ "www.cnsoft.com \");
Internet: SC ();
Fn (B );
Cin. get ();
}
The above Code uses several common methods to create an object. When a new object is created and its constructor is called, cout, a static member, runs the Add 1 operation, static member initialization should be performed before the main function is called and cannot appear in the class declaration. We can observe the running process and find that, the status of the static member count is not redefined with the creation of a new object. As a result, we understand that the static member of the class belongs to the class rather than to which object, therefore, the use of static members should be the class name plus the domain differentiation plus the member name. In the above Code, Internet: count, although we can still use object names by adding some operation symbols and member names, it is not recommended that static state class members belong to Classes rather than specific objects.
Static member functions are similar to the use of static members. They are also irrelevant to objects. The Calling method is class name, domain differentiation, and member function name. In the above Code, Internet is used :: SC (); static member functions cannot directly operate common members of a class because they have no relationship with objects.
If the above static void Internet: SC () is changed:
Static void Internet: SC () // static member function
{
Cout <name <endl; // Error
Cout <count <endl;
}
The difference between a static member function and a common member function lies in the lack of this pointer. Without this pointer, you naturally cannot know which object name is a member.
Based on the characteristics of static class members, we can briefly summarize the following points:
1. Used to save the number of objects.
2. As a flag, Mark whether some actions occur, such as the file opening status, printer usage status, and so on.
3. Store the memory address of the first or last member of the linked list.
In order to do some necessary exercises and thoroughly understand the significance of the existence of static objects, we described a Linear Linked List in the form of classes based on the previous structure tutorial, the Code is as follows:
# Include <iostream>
Using namespace std;
Class Student
{
Public:
Student (char * name );
~ Student ();
Public:
Char name [30];
Student * next;
Static Student * point;
};
Student: Student (char * name)
{
Strcpy (Student: name, name );
This-> next = point;
Point = this;
}
Student ::~ Student () // The Node detachment process
{
Cout <\ "destructor: \" <name <endl;
If (point = this)
{
Point = this-> next;
Cin. get ();
Return;
}
For (Student * ps = point; ps = ps-> next)
{
If (ps-> next = this)
{
Cout <ps-> next <\ "| \" <this-> next <endl;
Ps-> next = next; // = next can also be written as this-> next;
Cin. get ();
Return;
}
}
Cin. get ();
}
Student * Student: point = NULL;
Void main ()
{
Student * c = new Student (\ "marry \");
Student A (\ "Colin \");
Student B (\ "jamesji \");
Delete C;
Student * fp = Student: point;
While (FP! = NULL)
{
Cout <FP-> name <Endl;
Fp = FP-> next;
}
Cin. Get ();
}
From the code above, a linked list entry global pointer required by simple structured programming is replaced by the static member pointer of the class here (the static member of the class can completely replace the global variable ), the focus of this example is to observe the Destructor order of class members. Through understanding the Destructor order, you can use the destructor to perform node leeching.
Static members are proposed to solve the problem of data sharing. There are many ways to achieve sharing, such as setting global variables or objects. However, global variables or objects have limitations. In this chapter, we mainly introduce static members of the class to share data.
Static Data Member
In a class, static members can share data between multiple objects. Using static data members does not undermine the hidden principle, which ensures security. Therefore, static members are shared among all objects of the class, rather than members of an object.
Static data members can save memory because they are public to all objects. Therefore, for multiple objects, static data members can only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.
The usage and precautions of static data members are as follows:
1. When defining or describing static data members, add the keyword static.
2. static member Initialization is different from general data member initialization. The format of static data member Initialization is as follows:
<Data type> <class name >:< static data member name >=< value>
This indicates:
(1) initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects.
(2) during initialization, the access control letter private and public of the member are not added.
(3) during initialization, the scope operator is used to indicate the class to which it belongs. Therefore, static data members are members of the class, not members of the object.
3. static data members are stored in static mode, which indicates the lifetime of static data and must be initialized.
4. When referencing static data members, use the following format:
<Class name >:: <static 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 preceding format.
The following example illustrates the application of static data members:
# Include
Class Myclass
{
Public:
Myclass (int a, int B, int c );
Void GetNumber ();
Void GetSum ();
Private:
Int A, B, C;
Static int Sum;
};
Int Myclass: Sum = 0;
Myclass: Myclass (int a, int B, int c)
{
A =;
B = B;
C = c;
Sum + = A + B + C;
}
Void Myclass: GetNumber ()
{
Cout <\ "Number = \" <}
Void Myclass: GetSum ()
{
Cout <\ "Sum = \" <}
Void main ()
{
Myclass M (3, 7, 10), N (14, 9, 11 );
M. GetNumber ();
N. GetNumber ();
M. GetSum ();
N. GetSum ();
}
The output result shows that the Sum value is equal to the M object and N object. This is because when the M object is initialized, the Sum of the values of the three int-type data members of the M object is assigned to Sum, so Sum saves the value. When initializing the N object, Sum the values of the three int-type data members of the N object and add them to the existing values of Sum. Therefore, Sum will save the other values. Therefore, the values referenced by object M and object N are the same, that is, 54.
Static member functions
Static member functions, like static data members, all belong to static members of the class and are not object members. Therefore, you do not need to use the object name to reference static members.
In the implementation of static member functions, non-static members described in the class cannot be directly referenced. Static members described in the class can be referenced. If a non-static member is to be referenced in a static member function, it can be referenced through an object. The following is an example to illustrate this.
# Include
Class M
{
Public:
M (int a) {A = a; B + = ;}
Static void f1 (M m );
Private:
Int;
Static int B;
};
Void M: f1 (M m)
{
Cout <\ "A = \" <cout <\ "B = \" <}
Int M: B = 0;
Void main ()
{
M p (5), Q (10 );
M: f1 (P); file: // The object name is not used during the call.
M: f1 (Q );
}
You can analyze the results by yourself. It can be seen that the following format is used to call a static member function:
<Class name >:< static member function name> (<parameter table> );