13.C + + - static member variable, static member function

Posted by gotry on Sat, 04 Apr 2020 22:47:47 +0200

First, review the member variables

Can access public member variables through object name

The member variables of each object are exclusive

Member variables cannot be shared between objects

 

Let's talk about the static member variables of a class

introduce

  • Static member variables belong to the entire class
  • The lifetime of a static member variable does not depend on any object (like a global variable)
  • You can directly access public static member variables through the class name
  • Static member variables of all object sharing classes
  • Public static member variables can be accessed by object name

Characteristic

  • When defined in a class, it is directly modified by the static keyword
  • Static member variables need to allocate space independently outside the class
  • Static member variables are in the global data area inside the program

For example:

class Test{

private:
     static int  cnt;              //Static decoration tells the compiler that there is a static member variable cnt in the Test class

};

int Test::cnt = 0;         //Assign a value outside the class to allocate space

int main()
{
 Test::cnt = 100;                      //Set the public static member variable cnt=100
 printf("cnt:%d\n",Test::cnt);    
 return 0;
}

Let's take a small example

When the program is running, the number of objects of a certain class can be queried at any time

Ensure the safety of the program, and do not use global variables

The code is as follows:

#include "stdio.h"

class Test{

private:
     static int  cnt;      

public:
       Test()
       {
              cnt++;
       }
       ~Test()
       {
              cnt--;
       }
       int getCNT()
       {
              return cnt;
       }
};

int Test::cnt = 0;         //Assign a value outside the class to allocate space

int main()
{
       Test t1;
       Test t2;
       Test *t3;

       printf("count:%d\n",t1.getCNT());
       printf("count:%d\n",t2.getCNT());   

       t3= new Test;
       printf("count:%d\n",t1.getCNT());

       delete t3;
       printf("count:%d\n",t1.getCNT());
     
       return 0;
} 

Run print:

count:2
count:2
count:3
count:2

After running, you can find that you can count the number of objects

But there are also disadvantages. When calling the getCNT() function, there must be an object

If there is no object in the program, the count value cannot be queried at all

So we also need to use the static member function of the class to improve it

 

Static member function

introduce

  • Static member functions belong to the entire class
  • Static member function has no implicit this pointer and cannot directly access non static member variables
  • Public static member functions can be accessed by class or object names

Static member function definition is similar to member function, the only difference is that static keyword should be added before definition in class

For example:

class Test{

public:
       static int func()  {                //Define func static member functions
       //... ...
       return 0;
    }             
}

Or:

class Test{

public:
       static int func();        //Define func static member functions

}

int Test::func() 
{
//... ...
return 0;
}

  

Next, improve the above example, and use the static member function to obtain the number of statistics

#include "stdio.h"

class Test{
private:
     static int  cnt;      
public:
       Test()
       {
              cnt++;
       }

       ~Test()
       {
              cnt--;
       }

    static int getCNT()
       {
              return cnt;
       }
};

int Test::cnt = 0;         //Assign outside class,Make it allocate space

int main()
{
       printf("count:%d\n",Test::getCNT());
       Test t1;
       Test t2;
       Test *t3;
       printf("count:%d\n",Test::getCNT());

       t3= new Test;
       printf("count:%d\n",Test::getCNT());

       delete t3;
       printf("count:%d\n",Test::getCNT());
       return 0;
}

Run print:

count:0
count:2
count:3
count:2

Topics: C++