c++ primer note

静态变量

2017-02-21  本文已影响4人  lusoer

静态变量的三种形式:

static with external linkage:在所有关联文件中都有作用域

int max = MAX_INT-100;
void fun1(){...}
void fun2(){...}
...//只需声明在所有函数块之外就是external linkage

适用于多文件联编的程序中,是真正意义上的全局变量


static with internal linkage:只在它声明的那个文件中拥有全局作用域

static int max = 2000;
void fun1(){...}
...//声明在所有函数块之外,并加上static修饰

只在单个文件是全局变量


static with non-linkage:声明在特定的block中,比如函数块中,只能作用于函数块内,但是在整个程序的执行过程中不会expire。

void fun1()
{static int a = 2;
...}//声明在块内用static修饰
上一篇 下一篇

猜你喜欢

热点阅读