Static member variables of classes in C++.

Posted by mahers on Mon, 20 Sep 2021 22:26:33 +0200

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++:

  
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class test
  5. {
  6. private:
  7. static int m_value; //Define static member variables for classes
  8. public:
  9. static int getValue() //Define static member functions for classes
  10. {
  11. return m_value;
  12. }
  13. };
  14. int test::m_value = 12; //Static member variables of a class need to allocate memory space outside the class
  15. int main()
  16. {
  17. test t;
  18. cout << t.getValue() << endl;
  19. system( "pause");
  20. }

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:
 

  
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class test
  5. {
  6. private:
  7. static int m_value1; //Define static member variables for private classes
  8. public:
  9. static int m_value2; //Define static member variables for private classes
  10. public:
  11. test()
  12. {
  13. m_value1++;
  14. m_value2++;
  15. }
  16. int getValue() //Define static member functions for classes
  17. {
  18. return m_value1;
  19. }
  20. };
  21. int test::m_value1 = 0; //Static member variables of a class need to allocate memory space outside the class
  22. int test::m_value2 = 0; //Static member variables of a class need to allocate memory space outside the class
  23. int main()
  24. {
  25. test t1;
  26. test t2;
  27. test t3;
  28. cout << "test::m_value2 = " << test::m_value2 << endl; //Get the number of objects by calling public static member variables directly from the class name
  29. cout << "t3.m_value2 = " << t3.m_value2 << endl; //Get the number of objects by calling public static member variables directly from the object name
  30. cout << "t3.getValue() = " << t3.getValue() << endl; //Get the number of objects by calling a common function with an object name
  31. system( "pause");
  32. }

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
 

  
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class test
  5. {
  6. private:
  7. static int m_value; //Define static member variables for private classes
  8. public:
  9. test()
  10. {
  11. m_value++;
  12. }
  13. static int getValue() //Define static member functions for classes
  14. {
  15. return m_value;
  16. }
  17. };
  18. int test::m_value = 0; //Static member variables of a class need to allocate memory space outside the class
  19. int main()
  20. {
  21. test t1;
  22. test t2;
  23. test t3;
  24. cout << "test::m_value2 = " << test::getValue() << endl; //Get the number of objects by calling public static member functions directly from the class name
  25. cout << "t3.getValue() = " << t3.getValue() << endl; //Get the number of objects by calling a static member function on the object name
  26. system( "pause");
  27. }

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

Topics: C++ C#