C + + static member variable


executive summary

  • Static member variables belong to a class, not a specific object
  • Static member variables must be initialized outside the class declaration.
  • Static member variables allocate memory during initialization and release memory at the end of the program.
project content
key word static
access control yes
initialization Out of class initialization
Memory allocation timing On initialization
Time to release memory At the end of the program
Memory allocation location Global data area


General concept

Static member variable is a kind of special member variable, which is called keywordstaticmodification.

class Student {
    string getName() { return name; }

    Static int total; // static member variable
    string name;

The student class declares a public static member variable total, which indicates the total number of students. This variable is shared by this class. No matter how many student objects are created, there is only one total variable in memory.

Static member variables belong to a class, not a specific object.Like ordinary member variables, static member variables also have access control of private, protected and public.


Static member variables must be initialized outside the class declaration(except for one exception, I will talk about it later). Take the class just mentioned as an example, the specific initialization form is as follows:

int Student::total=0;
Type class name: variable name = value; (note that static is not necessary)
  • Initial value can be assigned or not assigned during initialization. If the value is not assigned, it will be initialized to 0 by default. All variables in the global data area have a default initial value of 0, while the default values of variables in the dynamic data area (heap area and stack area) are uncertain and generally considered as garbage values.
  • Static member variables modified by private, protected and public can be initialized in this way.

It should be noted that,Static member variables allocate memory at initialization time.Not when a class is declared or when an object is created. Therefore,Static member variables that are not initialized cannot be usedBecause memory has not been allocated.

Access method

  • The static member variable does not occupy the memory of the object,So as long as it is initialized out of class, it can be accessed even without creating an object
  • It can be accessed by object name or class name, but it should follow the access restrictions of private, protected and public keywords.
//Accessing static member variables through classes
Student::total = 10;
//Accessing static member variables through objects
Student stu;
stu.total = 20;
//Accessing static member variables through object pointers
Student *p_stu = new Student();
p_stu -> total = 20;

An exception that can be initialized within a class

typestatic const intIt can be initialized in class as follows.

class Student {
    string getName() { return name; }

    Static const int total = 1; // initialization within class
    string name;

However, if it is initialized inside the class, do not do it outside the class again, otherwise it will be repeatedly defined.