C++中static存储类型

来源:互联网 发布:淘宝动态评分查询工具 编辑:程序博客网 时间:2024/04/29 23:58

static:


When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

1.一个使用static关键字声明的变量将会拥有一个静态的生命周期(在程序启动时候分配空间,在程序结束时候释放空间);

2.一个使用static关键字声明的变量,若没有提供其他初始化值,将会被初始化为0。

3.在一个文件域中,使用static声明的函数或者变量具有内部链接属性(在定义文件之外的其他文件是不可见的).


When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.Static data members of classes must be initialized at file scope.

4.当在一个类的声明中具有一个static的数据成员,那么该static数据成员将对该类所有的实例只有一份拷贝(所有实例共享这一个数据成员,而非所有成员均有一份拷贝)。

5.当在一个类的声明中,使用static修饰的成员函数将只能够访问该类声明的静态成员,而不能够访问其他成员。

6.类中声明的静态成员必须在文件域中初始化。


The members of a union cannot be declared as static. An anonymous union declared globally must be explicitly declared static.

7.一个联合体的成员不能够被声明为static。但是一个全局的匿名联合体需要显示的指明其为static。


Objects and variables defined outside all blocks have static lifetime and external linkage by default. A global object or variable that is explicitly declared as static has internal linkage

8.对象或者变量若在所有块的外部定义,他们都将默认拥有一个静态的生命周期(程序开始创建,程序结束销毁),并且默认的具有外部链接性(在定义文件外是可见的)。但是若一个对象或者变量被显示的指定为static,那么它将具有内部连接性。


A variable declared static in a function retains its state between calls to that function.

9.一个被声明为static的变量在该函数被多次调用过程间都将保持它的状态(见static1.cpp)


In recursive code, a static object or variable is guaranteed to have the same state in different instances of a block of code.

10.在递归代码中,一个静态的对象或者变量将被确保在一个代码块的不同实例间保持相同的状态。

 

一个函数中声明的静态变量:

// static1.cpp// compile with: /EHsc#include <iostream>using namespace std;void showstat( int curr ) {   static int nStatic;    // Value of nStatic is retained                          // between each function call   nStatic += curr;   cout << "nStatic is " << nStatic << endl;}int main() {   for ( int i = 0; i < 5; i++ )      showstat( i );}

类中的成员使用static:

// static2.cpp// compile with: /EHsc#include <iostream>using namespace std;class CMyClass {public:   static int m_i;};int CMyClass::m_i = 0;int main() {   cout << CMyClass::m_i << endl;   cout << CMyClass::m_i << endl;   CMyClass::m_i = 1;   cout << CMyClass::m_i << endl;   cout << CMyClass::m_i << endl;}

局部static变量:

// static3.cpp// compile with: /EHsc#include <iostream>using namespace std;struct C {   void Test(int value) {      static int var = 0;      if (var == value)          cout << "var == value" << endl;      else         cout << "var != value" << endl;      var = value;   }}; int main() {   C c1;   C c2;   c1.Test(100);   c2.Test(100);}


原创粉丝点击