In C language, we know that there are static variables, and the life cycle and scope are different from those of ordinary variables. In C++ classes, there are also static member variables and static member functions. First, look at the syntax of static member variables and static member functions in C++:
#include <iostream> #include <string> using namespace std; class test { private: static int m_value; //Define static member variables for classes public: static int getValue() //Define static member functions for classes { return m_value; } }; int test::m_value = 12; //Static member variables of a class need to allocate memory space outside the class int main() { test t; cout << t.getValue() << endl; system( "pause"); }
In the above code, we define a static member variable and a static member function in the test class, respectively. First, let's look at the static member variable
- Static member variables are owned by the entire class
- The lifetime of a static member variable is independent of any object and is the lifetime of the program
Public static member variables can be accessed directly through the class name
- Static member variables of all object sharing classes
Public static member variables can be accessed through object names
- Static member variables require separate allocation of space outside the class
- Static member variables are in the global data area inside the program (Type className::VarName = value)
For the above points of static member variables, we modify the code above to count the number of current objects:
#include <iostream> #include <string> using namespace std; class test { private: static int m_value1; //Define static member variables for private classes public: static int m_value2; //Define static member variables for private classes public: test() { m_value1++; m_value2++; } int getValue() //Define static member functions for classes { return m_value1; } }; int test::m_value1 = 0; //Static member variables of a class need to allocate memory space outside the class int test::m_value2 = 0; //Static member variables of a class need to allocate memory space outside the class int main() { test t1; test t2; test t3; cout << "test::m_value2 = " << test::m_value2 << endl; //Get the number of objects by calling public static member variables directly from the class name cout << "t3.m_value2 = " << t3.m_value2 << endl; //Get the number of objects by calling public static member variables directly from the object name cout << "t3.getValue() = " << t3.getValue() << endl; //Get the number of objects by calling a common function with an object name system( "pause"); }
Compile output:
From the output, it looks like we want the effect, but C++ focuses on encapsulation, there are two inaccuracies in the above code
1. Class name or object name can directly access member variables, that is, member variables can be modified directly by the outside world
2. We use a member function to get the current number of objects. It seems okay, but we have to define objects and call them through objects. Sometimes we don't want to define objects, but we can also use member functions in classes. This is the static member function of classes.
The member functions of a class have the following characteristics:
- Static member function is a special member function of a class
- Static member functions are owned by the entire class without this pointer
- Static member functions can only access static member variables and static member functions directly
- Public static member functions of classes can be accessed directly by the class name
Public static member functions of classes can be accessed through object names
- Define static member functions, using the static keyword modifier directly
Now let's modify the code above
#include <iostream> #include <string> using namespace std; class test { private: static int m_value; //Define static member variables for private classes public: test() { m_value++; } static int getValue() //Define static member functions for classes { return m_value; } }; int test::m_value = 0; //Static member variables of a class need to allocate memory space outside the class int main() { test t1; test t2; test t3; cout << "test::m_value2 = " << test::getValue() << endl; //Get the number of objects by calling public static member functions directly from the class name cout << "t3.getValue() = " << t3.getValue() << endl; //Get the number of objects by calling a static member function on the object name system( "pause"); }
Compile output:
This allows us to access static member functions directly through the class name and get the number of objects without any objects.
Static member functions are very powerful in C++, including the singleton and second-order construction modes described later, which use static member functions and static member variables.
The figure below shows a comparison of static member functions and normal member functions