在模拟代码中使用全局变量
可能的重复:
全局变量不好吗?
我正在编写一个利用材料和能源特定数据的模拟代码。这些数据存储在全局数组中,因为一旦上传,它们就会在模拟过程中使用,并且应该可以被大多数函数访问。
我到处都读到使用全局变量不是一个好习惯。 有人可以向我解释一下,或者向我指出网络上的材料,解释如何在需要使用大量数据数组时避免在模拟应用程序编码中使用全局数组。我尝试用 C++ 编写代码,并尽可能多地使用面向对象的功能。
预先感谢您的帮助。
Possible Duplicate:
Are global variables bad?
I am writing a simulation code making use of material and energy specific data. This data is stored in global arrays, because once uploaded, they are used during the simulation and should be accessible by most of the functions.
I read everywhere that it is not good practice to use global variables.
Could someone explain me or point me to material on the web explaining how I could avoid using global arrays in simulation application coding while massive data arrays need to be used. I try to code in C++ and make use as much as possible of object oriented features.
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您说得对,不建议使用全局变量。您可以在
命名空间
内声明那些不相关的golbals,并在
.cpp
文件中定义它们。现在将它们用作
Globals::a
、Globals::d
等。我的答案是从代码管理的角度来看。You are right about the fact that, using globals are not recommended. You can declare those unrelated golbals inside a
namespace
,and define them in a
.cpp
file.Now use them as
Globals::a
,Globals::d
etc. My answer is in code management perspective.是的,你是对的,全局变量不好。
这是一个有用的链接,它解释了为什么全局变量不好以及如何避免它们。
http://c2.com/cgi/wiki?GlobalVariablesAreBad
编辑:
@sergio's post也指向相同的链接,你可以忽略这个答案
Yes you are right, global variables are not good.
This is a useful link which explains why global variables are bad and how to avoid them.
http://c2.com/cgi/wiki?GlobalVariablesAreBad
EDIT:
@sergio's post also points to the same link, you can ignore this answer
与通常避免全局变量的方式相同:使用局部变量。将数据输入函数的自然方法是将其作为参数传递。这并不昂贵,特别是如果您在适当的情况下通过引用传递的话。
The same way you avoid globals in general: by using locals instead. The natural way to get data into a function is to pass it as a parameter. This is not expensive, especially if you pass by reference where appropriate.
请查看这篇关于全局变量的文章。这是摘录:
它还讨论了几种替代方案。可能在您的情况下,您可以考虑:
隐藏全局变量(例如私有静态变量);
有状态过程:setter 和 getter 函数允许访问数组,同时也“屏蔽”它;
单例模式。
编辑:
我知道开发社区的一部分反对使用单例模式。我完全尊重这个意见。无论如何,在当前讨论的背景下,单例与全局变量的原始使用相比具有几个优点:
改进的访问控制;
同步的机会;
抽象实现的能力。
在这方面,一组 setter/getter 函数并没有更好,但也不算更差。我把选择如何处理他自己的代码的艰巨任务留给了OP。 (顺便说一句,本文讨论了更多方法,例如上下文对象、依赖注入等)。
Have a look at this article about global variables. This is an excerpt:
It also discusses several alternatives. Possibly in your case, you could consider:
hiding your globals (e.g., private static variables);
stateful procedures: setter and getter functions allowing access to the arrays while also "masking" it;
the singleton pattern.
EDIT:
I understand that a part of the development community are against the use of the singleton pattern. I fully respect this opinion. Anyway, in the context of the present discussion, the singleton offers several advantages over the raw use of globals:
improved access control;
opportunity for synchronization;
ability to abstract away the implementation.
In this respect, it is not better from a setter/getter set of functions, but still, not worse. I leave to the OP the hard task of choosing what to do with his own code. (BTW, the article discusses more approaches, like Context Objects, DependencyInjection, etc).
在代码中引入全局状态可能会使以多线程方式执行操作变得困难。
我还认为它会使代码的意图更难以遵循。如果将所有参数作为参数传递给函数,至少可以清楚函数可以访问哪些数据以及哪些数据可能会更改。使用全局变量并没有给阅读代码的人这个机会......
使用全局变量通常不会更快。如果您有需要传递给函数的大型对象,请通过引用传递这些参数,并且复制不会出现任何问题。
如果不了解更多有关您的设置的信息,就很难提出任何建议,但如果您有大量数据需要传递给一系列例程,我会很想将其全部放入一个结构中code>,并通过引用传递该
struct
:如果您只需要以只读方式访问数据,最好作为
const
引用传递:编辑: 对于你的评论,我不是谈论“全球”结构。您需要声明一个本地 struct 变量,将文件中的数据读取到 struct 中,然后通过引用将其传递给需要调用的任何函数:
Introducing global state into your code can make it difficult to do things in a multi-threaded way.
I would also argue it can make the intent of your code more difficult to follow. If you pass all of the arguments to a function as parameters at least it's clear what data the function has access to, and what has the potential of changing. The use of global variables doesn't give someone reading the code this chance...
It's also not generally true that using global variables is in any way faster. If you have large objects that you need to pass to functions, pass these arguments via references and there won't be any issues with copying.
Without knowing more about your setup, it's difficult to make any recommendations, but if you have a large amount of data that needs to be passed around to a series of routines I would be tempted to put it all in a
struct
, and to pass thatstruct
by reference:If you only need to access the data in a read-only fashion, it's best to pass as a
const
reference:EDIT: In answer to your comment, I'm not talking about a "global" structure. You would need to declare a local
struct
variable, read the data from your file into thestruct
and then pass it by reference to any functions that need to be called: