您的位置:首页 > 其它

How to define global variable MFC 中全局变量的定义

2008-11-18 17:45 363 查看
1.We could define a global variable in Stafx.h.

2.But for the sake of readability,Open two files in Notepad,save as .cpp(source file) and .h(header file) files,in the .cpp file we define the global variable ,in .h declare as external variable,and include the .h header file in the required class .
eg:

In Globalsv.cpp // this is the .cpp file

#include "Globalsv.h" //include the header file
int gv; //define the global variable here

In Globalsv.h declare the external variable
extern int gv; //this is the .h file and we declare the external variable here
and then,and don't forger to add Globalsv.h and Globalsv.cpp to the project.

At last you can use the global variable gv in any class you want to by including the header file Globalsv.h in your class.

chinese:

1可以在stdafx.h中定义

2但为了可读性,可以打开两个记事本,分别保存为.cpp和.h文件,在.cpp中定义,在.h中声明为外部变量,在所需要的类中包含.h头文件
eg:

在Globalsv.cpp中
#include "Globalsv.h"
int gv;

在Globalsv.h中声明为外部变量
extern int gv;

然后,将Globalsv.cpp和Globalsv.h添加到工程,
然后就可以在任意你想用变量gv的类的前面包含Globalsv.h
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: