如何使用全局变量

类别:编程语言 点击:0 评论:0 推荐:

这本来是我的一个问题,谢谢ThinkX、blow_jj 、Maconel 、hy1080 的解答,现整理一下答案。

使用全局变量应该将变量定义在.c中,在.h中extern。在需要使用变量的地方,包含.h就行了。还要注意自己给.h文件加些预编译,可以直接新建unit,预编译会自动生成。


例如在global.cpp和global.h中定义全局变量
在global.cpp中这样写:
#include "global.h"
//--------------------
int        a;
bool       b;
long       c;
AnsiString d;

在global.h中这样写:
extern int        a;
extern bool       b;
extern long       c;
extern AnsiString d;

在要使用全局变量的地方#include "global.h"就行了

本文地址:http://com.8s8s.com/it/it26446.htm