什么是定义全局变量的最佳方法 [英] What's the best way to define global variable

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

问题描述




我是C ++的新手,我需要定义一些全局变量,这些变量应该是大多数类可以访问的
。与此同时,我不希望在大多数这些类中自由修改全局

变量。我知道有一个名为singleton的

模式可以或多或少地做这样的伎俩。我想知道

这是最好的方法(关于方便性和安全性),因为这个

是如此根本的东西,我相信大多数人都有一个根据你的
经验说。任何评论或重播将不胜感激。


大卫。

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don''t won''t the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say based on your
experiences. any comment or replay will be much appreciated.

David.

推荐答案

David WOO写道:
David WOO wrote:



我是C ++的新手,我需要定义一些全局变量,大多数类都可以访问这些变量。与此同时,我不希望在大多数这些类中自由修改全局变量。我知道有一种名为singleton的模式可以或多或少地做这样的伎俩。


单身人士遇到一个完全不同的问题。

我想知道这是最好的方法吗(关于方便和安全) ,

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don''t won''t the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick.
A singleton surfes a completely different problem.
I am wondering is
this the best way to do it (regarding the convenience and safety),




最好的方法是根本没有全局变量。


-

Karl Heinz Buchegger
kb******@gascad.at


David WOO写道:
David WOO wrote:


我是C ++的新手,我需要定义一些应该是大多数课程都可以访问。与此同时,我不希望在大多数这些类中自由修改全局变量。我知道有一种名为singleton的模式可以或多或少地做这样的伎俩。我想知道这是最好的方法(关于方便性和安全性),因为这是一个基本的东西,我相信大多数人都有基于你的发言权
经验。任何评论或重播将不胜感激。

大卫。
Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don''t won''t the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick. I am wondering is
this the best way to do it (regarding the convenience and safety), as this
is such a fundamental thing, I believe most of you have a say based on your
experiences. any comment or replay will be much appreciated.

David.




将全球数据拆分为类别。声明一个结构来存储每个

类别的数据。在您的类中,将const引用存储到每个结构

,该类的数据类需要访问。实际的对象可以在main()中创建并传递给类构造函数。如果您认为

您可能会不小心地多次实例化这些结构中的任何一种,或许在您睡觉的时候可能是b $ b,那么单身模式适合您;
否则我不认为你需要打扰它。


这不是设计课程的最佳方式,但我希望它有所帮助。 br />

问候,

巴斯特。



Split the global data into categories. Declare a struct to store each
category''s data. In your classes, store a const reference to each struct
whose data the class will need to access. The actual objects can be
created in main () and passed to your class constructors. If you think
you might accidentally instantiate any of these structs more than once,
perhaps while you''re sleeping, then the singleton pattern is for you;
otherwise I don''t think you need to bother with it.

This isn''t the best way to design classes, but I hope it helps.

Regards,
Buster.


有了这样的回复,我们少了一个新手担心。


Karl Heinz Buchegger写道:
And with a response like that, we have one less newbie to worry about.

Karl Heinz Buchegger wrote:

David WOO写道:

David WOO wrote:

你好,

我是C ++的新手,我需要定义一些全局变量,大多数类都可以访问这些变量。与此同时,我不希望在大多数这些类中自由修改全局变量。我知道有一种名为singleton的模式可以或多或少地做这样的伎俩。

Hi,

I am a newbie on C++, I need to define some global variables which should be
accessible to most classes. In the mean time, I don''t won''t the global
variables be modified freely at most of these classes. I know there is a
pattern called singleton can more or less do such a trick.



单身人员会遇到一个完全不同的问题。



A singleton surfes a completely different problem.

我想知道
这是最好的方法(关于方便性和安全性),
I am wondering is
this the best way to do it (regarding the convenience and safety),



最好的方法是根本没有全局变量。

-
Karl Heinz Buchegger
kb ****** @ gascad.at



这篇关于什么是定义全局变量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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