现代C ++中的全局变量 [英] Global variables in modern C++

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

问题描述

创建一个所有成员(属性,函数)都是静态的类的(目标)缺点是什么?特别是与使用命名空间相比?或者你宁愿创建全局变量/函数吗?

What are the (objective) disadvantages of creating a class where all members (attributes, functions) are static? In particular in comparison with the use of a namespace? Or would you rather create global variables/functions?

我喜欢创建静态属性,因为我发现它们更整洁。 (我知道他们来自哪里,等等)我不是很熟悉命名空间。我对全局变量不太满意,因为我不太熟悉C关键字,例如 extern static

I like creating static attributes because I find them "tidier." (I know exactly where they come from, etc.) I'm not very familiar with namespaces. And I'm not comfortable at all with global variables, because I'm not very familiar with C keywords such as extern and static.

此外,如果我们考虑类

class MyStaticClass
{
    private:

        static int x;
        static double y;

    public:

        static float s;
        static double weatherForecast(unsigned int, char);
};

和命名空间

namespace MyNamespace
{
    int x;
    double y;
    float s;
    double weatherForecast(unsigned int, char);
}




  1. 明智的)之间调用 MyStaticClass :: weatherForecast 和调用 MyNamespace :: weatherForecast

读取/写入 MyStaticClass :: s 和读取/写入 MyNamespace之间是否有差异:s

Are there differences (performance-wise) between reading/writing MyStaticClass::s and reading/writing MyNamespace::s?

如果使用类别而不是主要类型,上述问题的答案是否会改变?

Would any of the answers to the above questions change if classes were used instead of primary types?


推荐答案


类,其中所有成员(属性,函数)是静态的?

Is it "good practice" to create a class where all members (attributes, functions) are static?

这称为monostate,这取决于。

This is called "monostate" and it depends.


还是你想创建一个命名空间?

Or would you rather create a namespace?

有静态函数的类可以是模板参数,不能。另一方面,命名空间允许依赖于参数的查找,而类则更少。

A class with static functions can be a template argument, whereas a namespace cannot. On the other hand, namespaces allow for argument-dependent lookup, whereas classes - less so.


或者你宁愿创建全局变量/函数?

Or would you rather create global variables/functions?

一些东西是真正的全球性的,像标准流,Logger对象,事件循环引擎(线程特定的全局)。例如,在每次调用中传递Logger对象或将它们存储为成员变量的代码比必要的更为复杂。IMO

Some things are truly global, like the standard streams, Logger objects, event loop engines (thread-specific global). For example, code that passes Logger objects in each and every call or stores them as member variables is more complicated than necessary, IMO.

有一种常被引用的误解,动态初始化的翻译单位的顺序是未定义的,所以人们过度使用单身而不是纯全局性,以确保Singleton对象在其第一次使用之前初始化。但是,有一种称为 Schwarz计数器的便携式技术,用于初始化标准流( std :: cout 和朋友),这确保这些全局变量在第一次使用之前即使在 main

There is an often cited misconception that the order of dynamic initialization accross translation units is undefined, so people overuse Singletons instead of plain globals to make sure the Singleton object is initialized before its first use. However, there is a portable technique called Schwarz Counter that is used for initializing the standard streams (std::cout and friends), which makes sure that these globals are initialized before their first use even before main is entered.

回答您更新的问题:不,不,不。

Answers to your updated questions: no, no, no.

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

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