当前位置:网站首页>Static keyword explanation

Static keyword explanation

2022-06-25 23:49:00 smileapples

static Keywords are used to modify the storage and visibility of variables .

A local variable defined inside a function , When executing to the function definition , The compiler allocates storage space for it in the stack space , Free the space after the function is executed . that , If you want to save the value of this variable in the function until the next call , How to achieve ? The simplest and crudest way is to define a global variable , But global variables break the scope of variables : All functions can modify this variable .static Keyword can solve this kind of problem well .

Besides , You need a data object to serve a class rather than a specific variable , That is, this member is required to be inside the class , When it's not visible , It can be defined as static data .

Storage of static data

Use of static member variables in classes

#include <iostream>
#include <vector>
using namespace std;

class Test
{
    
public:
    /**  Use static variables to share data among multiple objects of the same class . static  Member variables belong to the class , Do not belong to a specific object ,  Even if you create multiple objects , Just for the sake of cout Allocate a share of memory , All objects use this data in memory . When an object is modified  cout, It also affects other objects . */
    static int count;
};

//  Static member variables cannot be initialized with static, But there must be a data type .
//  The memory of static member variables is not allocated when the class is declared nor when the object is created , Instead, it is allocated during initialization outside the class , That is, static member variables that are not initialized outside the class cannot be used .
int Test::count=9;  /*static Class objects must be initialized outside the class :static Modified variables exist before objects , therefore static Decorated variables should be initialized outside the class */
/*static Decorated member variables do not occupy memory in the object , Because it is not generated on the heap or stack with the object , It is generated in static storage */

/* Initial values can be assigned during initialization , You can also assign no value . If not assigned , Then it will be initialized to by default  0.  All variables in the global data area have default initial values  0, The dynamic data area ( Heap area 、 The stack area ) The default value of the variable is uncertain , It is generally considered to be garbage value . */

int main()
{
    
    Test t1;
    cout << t1.count << endl;
    Test t2;
    t1.count = 100; /*static Decorated class variables are shared by all objects , As long as one object changes static Static variables , The entire object will change */
    cout << t2.count << endl;
    cout<< t1.count <<endl; //  Subject object t2 After modification , All objects have been modified 
    
    /*static  Member variables do not occupy the memory of the object , Instead, open up memory outside all objects , It can be accessed through objects , You can also access through classes without creating objects .  say concretely ,static  Member variables and ordinary  static  Variables like , Allocate memory in the global data area in the memory partition */
    cout<< Test::count<<endl;
    Test *ts = new Test;
    cout<<ts->count<<endl;
}

The difference between global variables and global static variables

  • Global variables are externally linked by default , Its scope is the whole project , Global variables defined in one file are passed in another file extern Global variables can be used for declaration of global variables ;
  • Global static variables are explicitly used static Decorated global variables , The scope is this file , External file pass extern Statements cannot be used either ;

summary

  1. static Decorated local variables are initialized only once , And it extends the life cycle of local variables , It is not released until the program runs .
  2. static Decorated global variables , This variable can only be used in this file , even extern Nor can external statements .
  3. static Modified function , This function can only be called in this file , Can't be called by other files .
原网站

版权声明
本文为[smileapples]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252101405484.html