什么是声明一个全局变量的最好方法? [英] What is the best way to declare a global variable?

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

问题描述

在C ++中,假设您要声明一个全局变量供许多人使用。



我通常在cpp文件中使用declare和define,然后在其他cpp文件(而不是头文件)中使用extern。



我不喜欢这种方法,我正在考虑这些方面的东西:



在头文件中:



some_file.h

  Class MYGlobalClass 
{

};


MyGlobalClass& MyGlobalClassInstance()
{
static MYGlobalClass实例;
return instance;

}

编辑




  • 可用于多线程应用程序

  • 命名空间污染

  • 可能不需要是单例,



您的想法,建议和新想法是什么?

解决方案

在一个头文件中声明它(使用 extern )并将其定义在 .cpp (或任何其他扩展名)文件。你可以使用一个函数并返回一个静态变量的引用,比如你在其他 .cpp 文件中规避与其他这样的命名空间范围变量相关的构造顺序问题。但是请记住,不会保护你免受破坏顺序问题 - 这是从完全相反的顺序从构造(这些东西被称为静态初始化顺序fiasco。如果你使用像yours的功能,并将其放入标题,使其内联当函数被包含到多个 .cpp 文件中时,使函数的重新定义有效(逻辑上,函数仍然只有一次,因为它中的静态只会存在一次或者只是在头文件中声明它,但是在 .cpp 文件中定义它,删除它的内联!)。

  inline A& getA(){static A a; return a;} 

销毁订单的潜在问题可以通过使用 new

  inline A& getA(){static A * a = new A; return * a;} 

然而,它的析构函数不会被调用。如果你需要线程安全,你应该添加一个互斥锁,防止多个访问。 boost.thread 可能有这样的东西。


In C++, say you want to declare a global variable to be used by many. How do you do it?

I commonly use declare and define in cpp file, and then use extern in other cpp file (and not headers).

I don't like this approach, and I am considering something along these lines:

In a header file:

some_file.h

Class MYGlobalClass
{

};


MyGlobalClass& MyGlobalClassInstance()
{
   static MYGlobalClass instance; 
   return  instance;

}

Edit

Consider in the following contexts:

  • can be used in multi-threaded applications
  • namespace pollution
  • may NOT necessery be a singleton, as many instances of this might be created

What are your thoughts, suggestions, new ideas?

解决方案

Declare it in one header file (using extern), and define it in one .cpp (or whatever other extension) file. You may use a function and return a reference to a static variable like you showed to circumvent problems with construction order relative to other such namespace scope variables in other .cpp files. But remember that won't protect you from destruction order problems - which is in the exact reverse order from construction (these things are called "static initialization order fiasco". If you use a function like yours and put it into headers, make it inline to make the redefinition of the function valid when it is included into multiple .cpp files (logically, the function is still only apparent once, because the static in it will only exist once, not separately for each file it is included into). Alternatively just declare it in a header but define it in one .cpp file (but then, remove the inline from it!).

inline A& getA() { static A a; return a; }

The potential problems with destruction order can be circumvented by using new:

inline A& getA() { static A *a = new A; return *a; }

The destructor of it, however, will never be called then. If you need thread safety, you should add a mutex that protects against multiple accesses. boost.thread probably has something for that.

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

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