C++中的 static 关键字 作者:陈厚辉
//file1.cpp #include<iostream.h> void fn(); extern int n; void main() { n=20; cout << n << endl; fn(); } //file2.cpp #include<iostream.h> static int n; //定义静态全局变量,初始化为0; void fn() { n++; cout << n << endl; }
//file1.cpp void fn(); void staticFn() void main() { fn(); staticFn(); } //file2.cpp #include<iostream.h> static void staticFn(); void fn(); void fn() { staticFn(); cout << "this is fn() \n"; } void staticFn() { cout << "this is staticFn() \n"; }