C++中的 static 关键字,C++中的 static 关键字
【 tulaoshi.com - C语言心得技巧 】
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 中的变量n找不到定义,产生连接错误。 //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"; } 连接时,将产生找不到函数staticFn()定义的错误。来源:http://www.tulaoshi.com/n/20160129/1486013.html
看过《C++中的 static 关键字》的人还看了以下文章 更多>>