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

来源:互联网 发布:免费点歌软件 编辑:程序博客网 时间:2024/05/22 06:33

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

原创粉丝点击