为什么需要明确定义静态变量? [英] Why static variable needs to be explicitly defined?

查看:300
本文介绍了为什么需要明确定义静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类中:

class foo
{
public:
    static int bar; //declaration of static data member
};

int foo::bar = 0; //definition of data member

我们必须显式定义静态变量,否则将导致

We have to explicitly define the static variable, otherwise it will result in a

对'foo :: bar'的未定义引用

我的问题是:

请注意不是以前询问的对静态变量的未定义引用问题的重复。该问题旨在询问静态变量的显式定义背后的原因

Please note that this is NOT a duplicate of previously asked undefined reference to static variable questions. This question intends to ask the reason behind explicit definition of a static variable.

推荐答案

从C语言开始,就像C语言一样,都是基于独立翻译的原理构建的。每个翻译单元都是由 compilerproperty 独立编译的,而无需其他翻译单元的知识。整个程序只有在链接阶段稍后才组合在一起。链接阶段是 linker 看到 entire 程序的最早阶段(被视为 compilerproperty 准备的目标文件的集合) )。

From the beginning of time C++ language, just like C, was built on the principle of independent translation. Each translation unit is compiled by the compiler proper independently, without any knowledge of other translation units. The whole program only comes together later, at linking stage. Linking stage is the earliest stage at which the entire program is seen by linker (it is seen as collection of object files prepared by the compiler proper).

为了支持独立翻译的原则,每个具有外部链接的实体都必须在一个翻译单元中定义,并在只有一个翻译单位。用户负责在不同的翻译单位之间分配此类实体。它被视为用户意图的一部分,即,用户应决定哪个翻译单元(和目标文件)将包含每个定义。

In order to support this principle of independent translation, each entity with external linkage has to be defined in one translation unit, and in only one translation unit. The user is responsible for distributing such entities between different translation units. It is considered a part of user intent, i.e. the user is supposed to decide which translation unit (and object file) will contain each definition.

这同样适用于该类的静态成员。类的静态成员是具有外部链接的实体。编译器希望您以某个翻译单位定义该实体。此功能的全部目的是使您有机会选择该翻译单元。编译器无法为您选择它。同样,它也是您意图的一部分,您必须告诉编译器。

The same applies to static members of the class. Static members of the class are entities with external linkage. The compiler expects you to define that entity in some translation unit. The whole purpose of this feature is to give you the opportunity to choose that translation unit. The compiler cannot choose it for you. It is, again, a part of your intent, something you have to tell the compiler.

这不再像以前那样严格不久前,由于该语言现在旨在处理(并消除)大量相同的定义(模板,内联函数等),但是一个定义规则仍然植根于独立的原则翻译

This is no longer as critical as it used to be a while ago, since the language is now designed to deal with (and eliminate) large amount of identical definitions (templates, inline functions, etc.), but the One Definition Rule is still rooted in the principle of independent translation.

除上述内容外,在C ++语言中,定义变量的位置将确定其相对于其他变量的初始化顺序在同一个翻译单元中定义。这也是用户意图的一部分,即编译器在没有您帮助的情况下无法决定的事情。

In addition to the above, in C++ language the point at which you define your variable will determine the order of its initialization with regard to other variables defined in the same translation unit. This is also a part of user intent, i.e. something the compiler cannot decide without your help.

从C ++ 17开始,您可以将静态成员声明为 inline 。这消除了对单独定义的需要。通过以这种方式声明它们,您可以有效地告诉编译器您不在乎此成员的物理定义位置,因此也不必关心其初始化顺序。

Starting from C++17 you can declare your static members as inline. This eliminates the need for a separate definition. By declaring them in that fashion you effectively tell compiler that you don't care where this member is physically defined and, consequently, don't care about its initialization order.

这篇关于为什么需要明确定义静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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