So far, all the member variables and member functions in the class we have designed are objects, such as the book class we defined earlier, using the book class to declare two objects Alice and Harry, both of which have their own price and title member variables. It also owns all the member functions in the class.
In addition to this situation, we have another type of member, which is a member variable and member function that is combined with static. Once a member variable or member function in a class is combined with the static keyword, the member variable or member function belongs to the class, not to any other object, and of course any object can share the member variable and the member function.
Static member variables
Static member variable declarations are very simple, just add the static keyword to the front of the member variable declaration, as shown in Example 1, we declare a static member variable count in the example and set it to the private property. This count variable is designed primarily to count the number of currently surviving student class objects, and of course the class is not perfect, and through this example, we can understand the declarative syntax of static member variables.
Class student
{
Public
Student () {count + +;}
~student () {count--;}
Private
static int count;
Other member variables
};
Static member variables require definition operations after they are declared inside a class.
Example 2:
Class student
{
Public
Student () {count + +;}
~student () {count--;}
Private
static int count;
Other member variables
};
int student::count = 0;
Class student
{
Public
Student () {count + +;}
~student () {count--;}
Private
static int count;
Other member variables
};
int student::count = 0;
Please take a closer look at the code in Example 2, this code, although compared with example 1 only add a line of code, but there are several places we need special attention. First, the definition of a static member variable must be outside of any program block, and the next time you invoke the variable, you can add the domain resolver "::" with the name of the variable, which is a unique reference for static member variables, which is not required when defining outside the class.
In the C + + syntax, it is specified that static member variables are initialized to 0 by default, and the out-of-class definitions are optional. In fact, in some compilers, if you do not add the definition outside the class, there will be some unknown situation, so in the actual design of the program, it is best to add the class outside the definition.
Static member variables do not affect the size of the class and its objects, which means that the sizeof results are unaffected. In Example 2 above, regardless of whether we declare the static member variable of Count, sizeof (student) or sizeof (student object), the result will not change.
Static member variables belong to a class and do not belong to any one object, so you can implement data sharing functionality, as shown in Example 3.
#include <iostream>
using namespace Std;
Class Test
{
Public
static int num;
};
int test::num = 1;
Int main ()
{
Test one;
test;
Test three;
cout<<test::num<< "" <<one.num<& lt; " "<<two.num<<" "<<three.num<<endl;
Test::num = 5;
cout<<test::num<< <<one.num<< "<<two.num<<" <<three.num<< Endl
One.num = 8;
cout<<test::num<< <<one.num<< "<<two.num<<" <<three.num<< Endl
Two.num = 4;
cout<<test::num<< <<one.num<< "<<two.num<<" <<three.num<< Endl
Three.num = 2;
cout<<test::num<< <<one.num<< "<<two.num<<" <<three.num<< Endl
return 0;
}
Program Run Result:
1 1 1 1
5 5 5 5
8 8 8 8
4 4 4 4
2 2 2 2
In this example, to facilitate the sharing of style static member variables, we set the static member variable to the public property, which makes it easy to invoke outside of the class. Outside of the class we first define the static member variable and initialize it to 1. In the main function we define three objects for the test class, one, two, and three, respectively. The static member variable is then called with the class name and object name to modify its value, and the results of each call are printed. As you can see from the running results of the program, there are four methods that call static member variables whose values are equal, and if any of them modifies the static member variable, all other calls to the static member variable will be changed along with it. This is the shared attribute of static member variables, which do not belong to any object, but static member variables can be accessed through objects. Static member variables belong to a class, so static member variables can be called through a class. Static member variables, if set to private or protected properties, are also inaccessible outside the class, but are not subject to this limitation when defining the variable, as shown in Example 2, although the static member variable count is the private property, However, it is not subject to private restrictions when it is defined outside the class.
Static member functions
In addition to static member variables that can be declared with static within a class, static member functions can be declared with static member functions only, and only statically member variables are accessible.
Example 4:
#include <iostream>
using namespace Std;
Class Test
{
Public
Test (int A, int b) {num = A; plus = b;}
static int Getnum () {return num;}
static int Add () {return num+plus;}//compile Error
void Setnum (int a) {num = A;}
void Setplus (int a) {plus = A;}
Private
static int num;
int plus;
};
int test::num = 1;
int main ()
{
Test one;
One.setnum (5);
Cout<<test::getnum () <<endl;
return 0;
}
In this routine, the class test has a static member variable num, an ordinary member variable plus, both of which are private properties, the member function has a constructor, two ordinary member functions, and two static member functions. In the constructor, we refer to the static member variable num, which is allowed, and also in the Setnum function, which also references the static member variable num, which is also allowed. In the Add function, however, we not only refer to the static member variable num, but also access the non-static member variable plus, which is not allowed. Static member functions can access only static member variables, not non-static member variables. Ordinary member functions (including constructors and destructors) can access both normal member variables and static member variables.
There are two ways to access a static member variable and a static member function, one is the same as a normal member variable member function, accessed through an object, and the other is accessed by the class name plus the domain resolution operator. Of course, access is still subject to the private, protected, and public keyword access restrictions. The preferred method for accessing static member variables and static member functions is access through classes, after all, static member variables and static member functions belong to classes, associated with classes, and not objects belonging to classes. Ordinary member variables or member functions cannot be accessed through a class. Because static member variables and static member functions belong to a class and not to an object, the this pointer does not exist inside a static member function because the static member function does not belong to the object.
You can declare a static variable inside a static member function, noting that it is not a static member variable. If you declare a static variable inside a static member function, all objects of that class will share the variable.
Example 5:
#include <iostream>
using namespace Std;
Class Test
{
Public
static void Add (int a);
};
void Test::add (int a)
{
static int num = 0;
int count = 0;
num + = A;
Count + = A;
cout<<num<< "" <<count<<endl;
}
int main ()
{
Test One,two,three;
One.add (5);
Two.add (4);
Three.add (11);
return 0;
}
Program Run Result:
5 5
9 4
20 11
To illustrate the problem directly, we only declare a static member function add in class test and define the function outside of the class. Note that it is not necessary to define a static member function outside the class without the static keyword. Inside the Add function we define a static variable num and initialize it to 0, in order to enhance the contrast, we define a normal variable count. Three variables are defined in the main function, and the static member function is called, and the result of the NUM value printing is cumulative, and count starts at 0 each time and is not cumulative. By contrast, it is easy to see that static is shared by these three objects, with three objects in one piece of data.
C + + classes and static