当前位置:网站首页>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 .
边栏推荐
- One article explains R & D efficiency! Your concerns are
- Megacli常用命令整理
- DPVS-FullNAT模式keepalived篇
- The package name of the manifest file in the library project and the app project are not the same
- 说说单例模式!
- Style setting when there is a separator in the qcombobox drop-down menu
- 51 single chip microcomputer, some registers, some knowledge points
- phoenix索引
- proxy
- 实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
猜你喜欢
C# IO Stream 流(一)基础概念_基本定义
Qtcreator formatting code
7.常用指令(下)v-on,v-bind,v-model的常见操作
Uniapp -- list page of multi header tabs
Style setting when there is a separator in the qcombobox drop-down menu
YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
Binary, hexadecimal, big end and small end
SSL/TLS、对称加密和非对称加密和TLSv1.3
Line height for small use
随机推荐
mysql集群
产品经理如何把控产品开发的进度
Leetcode-1528- rearrange string - hash table - string
UE4 learning records create a role and control its movement
DPVS-FullNAT模式部署篇
6.常用指令(上)v-cloak,v-once,v-pre
oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑
Graduation trip | recommended 5-day trip to London
.user.ini文件导致的php网站安装问题
Database - mongodb
Binary, hexadecimal, big end and small end
excel如何实现中文单词自动翻译成英文?这个公式教你了
Hibernate architecture introduction and environment construction (very detailed)
第六章 习题(678)【微机原理】【习题】
常用的几款富文本编辑器
On the quantity control mechanism of swoole collaboration creation in production environment
推荐系统设计
Megacli常用命令整理
Uniapp -- the use of document collation and push of unipush
数据同步