Static member functions and static member variables in C++ (static)

  • 2020-05-24 05:56:27
  • OfStack

Static member functions and static member variables in C++ (static)

This paper introduces static member functions and static member variables. It is my reading notes. I hope it is short but comprehensive enough to review. If you have some C++ knowledge, it can help you recall it quickly.

Review the static keywords in the C language

(1) add it before the local variable to make it a static local variable. The scope is still inside the function, but the life cycle is longer.

(2) before the global variable is scoped to the file scope, which means that it cannot be scoped even if other files use the extern extension scope. This is useful in multi-person projects in the C language, which avoids the duplication of variable names. In C++, however, this functionality has been replaced by a namespace, but in order to remain compatible with the C language, static still has this functionality.

(3) before the function definition or declaration, the function scope is limited to the file scope, also to avoid multiple files with the same name function.

When the static keyword appears in the class

When static appears in the class definition, static member variables and static member functions appear. Static members belong to a class, not to an object. Even without any one instance, the static member variable of the class already exists and can be accessed through "class name :: member name." Static member functions of a class can be called in the same way, calling member methods before an instance of the class is generated, typically by implementing the singleton pattern.

(1) static member variables

Static member variables are essentially global variables, but it is easier to understand and maintain global variables that are closely related to certain classes by writing them into the class as a whole. So use static member variables as much as possible and reduce the use of global variables. Ordinary member variables each object has its own copy, but static member variable 1 has only one copy, which is Shared by all objects of this class. If you use the sizeof operator to calculate the size of an object, you get a result that does not include static member variables.

Static members are also limited by private, public, and so on.

A typical use of static member variables is to count the number of instances generated. The general idea is to set a static member variable named num and initialize it to 0, in the constructor ++num, and in the destructor --num. The value of num is then the number of current instances. In fact this also leads to a hidden bug. Look at the following code:


class CNum {
   public:
       static int num;
       ~CNum() { --num; }
       CNum() { ++num; }
};

int CNum::num = 0;
void fun(CNum n){ }

 int main() {
   CNum n;
   fun(n);
   fun(n);
   cout << CNum::num << endl;
  return 0;
}

Results: 1

num has become a negative number. Is the destructor called 1 more times than the constructor? Not really. When performing fun (n); The copy constructor is called in the statement, which is the default version provided by the compiler because we did not give the implementation. There is no ++num statement in the constructor, so the number is counted twice (fun(n) is called twice).

The solution is to provide your own copy constructor and add ++num to the body of the function.

(2) static member function

The static member function cannot call the non-static member function internally, because the non-static member function needs to pass in an this pointer, which makes the static member function very difficult. It does not know the relevant information, so it cannot provide the this pointer.

Initialization of static member variables

int CNum::num = 0; Is the initialization of a static member variable. This can be thought of as the definition of a static variable (which is initialized at the same time as the definition is defined, even if it is not initialized), while the class static int num; Viewed as a declaration, this understanding highlights the fact that static member variables are essentially global variables. Note the addition of "class name ::" to the definition outside the class.

For constant member variables, we know that 1 is always initialized with an initialization list, so how do we initialize a variable that is both a constant and a static member (modified by const and static)? Is static member variable 1 defined and initialized outside the class like 1, or is constant member variable 1 initialized with a list like 1? The former, defined and initialized outside the class, is declared inside the class, as follows:


class CNum {
   public:
      const static int num;
};

const int CNum::num = 0;

 int main() {
   CNum n;
  return 0;
}

In fact, const int can be considered as a data type, which has the same status as int1. It's good to think of it this way, as if going from const int to int requires type casting, and it's natural to think of them as two types. The corresponding const char and char should also be viewed as two types, as if they have no special relationship at all.

In addition, the static const int type and static const char type can be initialized directly within the class, which means that neither needs to be re-defined outside the class, as follows:


class CNum {
   public:
     const static int a = 19;
};


 int main() { 8   cout << CNum::a << endl;    // The output 19
  return 0;
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: