C++ 中的全局变量

发布于 2024-09-16 09:57:17 字数 256 浏览 10 评论 0原文

在具有许多类的 C++ 多线程应用程序中,我试图找出定义全局变量

  1. C 风格的方法是什么,在任何一个源文件中将其定义为全局变量,在标头中将其定义为 extern包含在访问该变量的类中。

  2. 编写一个 Singleton 类,其中包含这些全局变量并公开 set/get 方法来写入变量。

通过第二种方法,可以通过锁以集中方式控制多线程访问,而不是第一种方法。

还有更多更好的方法吗?

In a C++ multi-threaded application with many classes, i am trying to find out what are the methods to define a global variable

  1. C style, define it as global in any one source file, define it as extern in a header which is included in the classes that access this variable.

  2. Write a Singleton class, which contains these global variables and exposes set/get methods to write to the variable.

By second method one can control multi-threaded access via locks in a centralized manner rather than the first approach.

Are there more and better ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

再可℃爱ぅ一点好了 2024-09-23 09:57:17

首先,尽量避免使用全局变量。如果您只需要这样做(例如 cincoutcerr 就是这种情况),您的第二种方法绝对是最好的(并且更自然)的做法。

First of all try to avoid global variables as much as you can. If you just need to do it (by example this is the case with cin, cout and cerr) your second method is definitely the best (and more natural) way of doing it.

┈┾☆殇 2024-09-23 09:57:17

如果可以缩小“全局变量”的范围(通常是这种情况 - 有多少变量是真正的全局变量?),那么您可以将其设为相应所属类中的私有静态类成员。如果您的其他类需要查看它(或者不太可能更新它),请提供 get/put 访问器。

If the scope of your "global variable" can be narrowed down (which is typically the case - how many variables are truly global?) then you can make it a private static class member in the appropriate owning class. If your other classes need to see it (or less likely, update it), provide get/put accessors.

俯瞰星空 2024-09-23 09:57:17

我肯定会选择 Singleton 类。这是在多线程 OOP 环境中处理“全局”变量的最佳方法。

I would definitely go with the Singleton class. It's the best way to handle "global" variables in a multithreaded OOP environment.

陪你搞怪i 2024-09-23 09:57:17

如果您必须使用全局变量(为什么要使用全局变量?)我推荐您描述的第二种方法。第一种方法可能会遇到各种命名空间问题。

If you must use a global variable (and why are you using one?) I recommend the second way you described. The first way is the way you can run into all kinds of namespace problems.

过去的过去 2024-09-23 09:57:17

这个问题可以很容易地用替代方法解决。

C++ 通过其称为作用域解析运算符的新运算符 :: 非常轻松地解决了这个问题。语法如下

:: variable-name;

该运算符允许访问变量的全局版本。

This probleam can be solved with an alternatieve method very easily.

C++ resolves this problem very easily by its new operator :: called scope resolution operator. The syntax is as follows

:: variable-name;

This operator allows access the global version of a vriable.

走走停停 2024-09-23 09:57:17

人们往往更喜欢第二种方法,因为它似乎可以为您提供更好的控制,但在某些情况下可能不是很有用。

首先,根据我对 OOP 哲学的理解,我并不将对象视为一堆数据的集合,而是将实体视为可以代表现实世界问题的实体。所以我认为用一个类来存储随机数据不是一个好主意。特别是当数据成员基本上不相关时。

其次,如果您正在考虑使用中央控制,例如。使用单个互斥体来访问所有数据成员,这对于不相关的数据成员来说效果不佳。您将不必要地阻塞大量线程,而它们想要的数据并不完全是当前受锁保护的数据。

所以这可能看起来很奇怪,但我更喜欢第一种方法。

One tends to prefer the second method, because it seems to give you a better control but it may not turn out very useful in some scenarios.

First, In my understanding of philosophy of OOP, I do not consider objects as collections of bunch of data, but entities in terms of which, you can represent real world problems. So I do not consider it a good idea to to have a class to store random data. Especially when the data members are largely unrelated.

Second, If you are thinking of having a central control eg. having a single mutex to access all the data members, this is not going to work out very well for unrelated data members. You are going to block a lot of threads unnecessarily while the data they want is not exactly the one which is being currently protected by the lock.

So it may seem strange, but I would prefer the first method.

哆兒滾 2024-09-23 09:57:17

这取决于手头的问题。

C 风格的全局变量的优点是简单,不需要 Singleton::instance() 调用。但是,Singleton::instance() 允许您在第一次调用时初始化全局状态。

为了获得两全其美的效果,请使用使用 Schwarz Counter 方法初始化的 C 风格全局变量。 http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/ Nifty_Counter

It depends on the problem at hand.

C-style globals have the advantage of simplicity, no need for Singleton::instance() call. However, Singleton::instance() allows you to initialize your global state on the first call.

To get the best of both worlds use C-style globals initialized using Schwarz Counter method. http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter

请你别敷衍 2024-09-23 09:57:17

您可以定义一个使用句柄/主体惯用法包装单个实现的值对象。

另请参阅 Alexandrescu 的“Modern C++ Design”,其中讨论了在 MT 环境中实现单例的困难以及如何解决这些问题。

You can define a value object that wraps a single implementation with the handle/body idiom.

Also review "Modern C++ Design" by Alexandrescu for discussion about the difficulties of implementing singleton in MT environments and how to go about addressing them.

紧拥背影 2024-09-23 09:57:17

不是要踢死马,而是如前所述,避免全局变量是最好的解决方案。 此处列出了一些原因。如果必须使用全局变量,您可能需要考虑提供一个函数来访问它,以避免所谓的“全局初始化失败”。

Not to kick a dead horse but, as mentioned, avoiding globals is the best solution. Some reasons are listed here. If a global variable is a must you might want to consider providing a function to access it from to avoid the so called 'global initialization fiasco'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文