简体   繁体   English

为什么这些全局变量具有名称空间范围?

[英]Why are these global variables when they have namespace scope?

In the following code, this is calling the variables in the globals.cpp namespace actual global variables . 在以下代码中, 将调用globals.cpp命名空间中的变量实际的全局变量

globals.h globals.h

#ifndef GLOBALS_H_
#define GLOBALS_H_

namespace Constants
{
    // forward declarations only
    extern const double pi;
    extern const double avogadro;
    extern const double my_gravity;
}

#endif

globals.cpp globals.cpp

namespace Constants
{
    // actual global variables
    extern const double pi(3.14159);
    extern const double avogadro(6.0221413e23);
    extern const double my_gravity(9.2); // m/s^2 -- gravity is light on this planet
}

source.cpp source.cpp

#include <iostream>
#include <limits>

#include "globals.h"

int main()
{
    double value_of_pi = Constants::pi;

    std::cout << value_of_pi;

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

    return 0;
}

I know that extern is used to access global variables in other translation units. 我知道extern用于访问其他翻译单元中的全局变量。 I'm probably reading too much into this, but why are the variables in the globals.cpp namespace considered to be global when they have namespace scope? 我可能对此读得太多了,但是为什么当globals.cpp命名空间中的变量具有命名空间范围时,它们被认为是全局变量? Also, am I right to assume that Constants::pi retrieves the identifier pi from the forward declaration in the globals.h namespace? 另外,我是否可以假设Constants::pi pi从globals.h命名空间中的前向声明中检索标识符pi

This question is a continuation of a previous question that I asked here . 这个问题是我 在这里 问的先前问题的延续

Global roughly means accessible from every translation unit, no matter if the variable is in a namespace or not. 全局大致意味着可以从每个翻译单元访问,而不管变量是否在名称空间中。 The best example is std::cout , which is a global variable defined in namespace std , and which represents an instantiation of std::basic_ostream<> . 最好的示例是std::cout ,它是在namespace std定义的全局变量,它表示std::basic_ostream<>的实例。

Regarding your second question, Constants::pi is accessible because you include the header globals.h , which declares extern const double Constants::pi; 关于第二个问题,因为您包含标头globals.h ,所以可以访问Constants::pi该标头声明了extern const double Constants::pi; . This declaration instructs the compiler that the variable has external linkage, and it is your responsibility to define it in some .cpp file (which you do in globals.cpp ) 1) . 该声明指示编译器该变量具有外部链接,您有责任在某个.cpp文件中定义它(您在globals.cpp1 So the linker is able to find the symbol in the globals.cpp , and that's it. 因此,链接器可以在globals.cpp找到该符号。


1) Note that you can even provide the definition extern const double pi = 3.14; 1)请注意,您甚至可以提供定义extern const double pi = 3.14; directly in the header file, but it is not recommended, since including the header in multiple translation units will lead to a duplicate symbol. 直接在头文件中,但不建议这样做,因为将头包含在多个翻译单元中将导致符号重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM