声明全局变量 [英] declaring global variables

查看:119
本文介绍了声明全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为对话框类(对话框类的全局对象)创建全局变量,所有其他类(包括其他对话框类)都可以访问该变量?

谢谢.

How do I create global variable for dialog class (global object of dialog class), which is accessible to all other classes including other dialog classes?

Thank You.

推荐答案

我建​​议您实现 Singleton模式 [ ^ ].
I suggest you implement the Singleton Pattern[^].


定义,并在允许访问它的所有其他源文件中将其声明为extern.

Define it inside the dialog source file and declare it as extern in all the other source files allowed to access it, e.g.

// MyDialog.cpp
int g_counter = 0; // variable definition (and initialization, here)





// MyOtherDialog.cpp
extern int g_counter;
//...
g_counter = 5; // access global variable defined in MyDialog.cpp



:-)



:-)


如果需要的是您的应用程序中所有对话框都可以使用的全局变量...我会在您的应用程序类中声明一个变量...然后''将能够从任何地方访问它...

我会使用getters和setters来访问它...

HTH! :thumbsup:
If the need is a global variable available to all the dialogs in your app... I''d go for a variable declared in your app class... then you''ll be able to get access to it from everywhere...

I''d use getters and setters in order to acces to it...

HTH! :thumbsup:


这篇关于声明全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆