当前位置:网站首页>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
- 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 .
- static Decorated global variables , This variable can only be used in this file , even extern Nor can external statements .
- static Modified function , This function can only be called in this file , Can't be called by other files .
边栏推荐
- static关键字详解
- Jenkins releases PHP project code
- Stream in PHP socket communication_ Understanding of select method
- 7.常用指令(下)v-on,v-bind,v-model的常见操作
- Object类常用方法
- Qlabel text scrolling horizontally
- Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
- 今天说说String相关知识点
- 18亿像素火星全景超高清NASA放出,非常震撼
- 解析产品开发失败的5个根本原因
猜你喜欢

实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式

Hibernate core api/ configuration file / L1 cache details

为什么Integer的比较最好使用equals

如何进行流程创新,以最经济的方式提升产品体验?

SSM整合学习笔记(主要是思路)

idea 查看单元测试覆盖率

excel如何实现中文单词自动翻译成英文?这个公式教你了

Qtcreator formatting code

Online customer service - charging standards and service provision of third parties

Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
随机推荐
Analysis on the control condition and mode of go cooperation overtime exit
mysql集群
Anaconda一文入门笔记
CXF
php进程间传递文件描述符
先序线索二叉树
Analysis on resource leakage /goroutine leakage / memory leakage /cpu full in go
谈一谈生产环境中swoole协程创建数量控制机制
第六章 习题(678)【微机原理】【习题】
18亿像素火星全景超高清NASA放出,非常震撼
Uniapp -- framework arrangement and analysis summary
sqlServer2008中float类型数据与datetime类型数据互转
unsigned与signed之大白话
为什么Integer的比较最好使用equals
Graduation trip | recommended 5-day trip to London
Raspberry pie sends hotspot for remote login
Database - mongodb
后序线索二叉树
Online customer service - charging standards and service provision of third parties
line-height小用