min*_*nda 15 c++ static namespaces const
// MyClass.h
namespace MyNamespace {
static const double GasConstant = 1.987;
class MyClass
{
// constructors, methods, etc.
};
}
Run Code Online (Sandbox Code Playgroud)
我以前在MyClass声明中声明了GasConstant(并且在源文件中有一个单独的定义,因为C++不支持const非整数类型的初始化).然而,我需要从其他文件访问它,从逻辑上看,它似乎应该驻留在命名空间级别.
我的问题是,static const在这种情况下会产生什么影响?显然const意味着我无法为GasConstant分配新值,但命名空间中的静态成员意味着什么.这类似于文件范围内的静态,其中成员在单元外是不可访问的吗?