CPU型号:Intel(R) Core(TM) i5-2450M
系统:windows 10
IDE:Microsoft Visual C++ 6.0(下文中简称VC)
制图软件:Photoshop cs5
预备知识:头文件和其对应的实现文件.c或者.cpp可视为一个模块)
今天在测试static关键字的时候,发现了一个奇怪的现象,也就是在头文件中定义static变量竟然可以被包含这个头文件的其他模块引用!下面是测试代码:
头文件A.h
//A.h
#ifndef _A_H
#define _A_H
static int a = 5;
#endif
main函数main.cpp
#include <IOSTREAM>
#include "A.h"
using namespace std;
int main(void)
{
cout<<"a="<<a<<endl;
return 0;
}
编译没有问题!!!运行也没有问题!!!还能显示出结果a=5,说好的static变量对其他模块隐藏呢!!!???怎么会这样???难道static变量放在头文件中意义就变了吗??
新建一个模块再次进行测试
模块B.h
#ifndef _B_H
#define _B_H
void Bfun(void);
#endif
模块B的实现代码B.cpp
#include "B.h"
#include "A.h"
#include <IOSTREAM>
using namespace std;
void Bfun(void)
{
a++;
cout<<"Bfun: a="<<a<<endl;
}
修改后的main.cpp
#include <IOSTREAM>
#include "A.h"
#include "B.h"
using namespace std;
int main(void)
{
Bfun();
cout<<"main: a="<<a<<endl;
return 0;
}
运行结果:
怀疑人生了有没有????!!!static变量又不能对其他模块进行隐藏,又不是全局变量,这又是怎么回事?打印下地址看看
两个不一样的地址,说明a不是全局变量,也就是说定义在头文件里的static变量,被其他模块每包含一次就申请一次内存!
如果把A.h中的static去掉呢?
编译运行会发现以下错误提示:
static int a去掉static后就变成全局变量了,此时又提示重复定义,由此可见,定义在头文件中的全局变量,每被#include一次,就会被声明定义一次。全局变量#include多次会出错,而static变量被#include多次会生成多个局部变量,但这并不是我们想要的结果!
如果把变量声明在.cpp文件中呢?
A.h
#ifndef _A_H
#define _A_H
#endif
A.cpp
#include "A.h"
static int a = 5;
main.cpp
#include <IOSTREAM>
#include "A.h"
using namespace std;
int main(void)
{
cout<<a<<endl;
return 0;
}
编译后会提示:
error C2065: 'a' : undeclared identifier
这样的结果刚好也符合static变量对其他模块是不可见的特性。
去掉static后,再进行测试。
结果怎么又是未定义error C2065: 'a' : undeclared identifier
原来是引用全局变量要在引用的模块内加extern关键字!
再在main.cpp的main函数前加上extern int a ;即可对int a进行引用。修改代码:
A.h
#ifndef _A_H
#define _A_H
#endif
A.cpp
#include "A.h"
int a = 5;
B.h
#ifndef _B_H
#define _B_H
void Bfun(void);
#endif
B.cpp
#include "B.h"
#include "A.h"
#include <IOSTREAM>
using namespace std;
extern int a;
void Bfun(void)
{
a++;
cout<<"Bfun: a="<<a<<",addr="<<&a<<endl;
}
main.cpp
#include <IOSTREAM>
#include "A.h"
#include "B.h"
using namespace std;
extern int a;
int main(void)
{
Bfun();
cout<<"main: a="<<a<<",addr="<<&a<<endl;
return 0;
}
运行结果:
在模块B和main函数中,全局变量a的地址和值保持一致!
因此!
变量不要在头文件中定义,要在实现文件中定义!
全部变量的引用要加关键字extern!