In the following code, this is calling the variables in the globals.cpp namespace actual global variables.
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
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
#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. 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? Also, am I right to assume that Constants::pi retrieves the identifier pi from the forward declaration in the globals.h namespace?
This question is a continuation of a previous question that I asked here.