static const 对命名空间成员有什么影响
// MyClass.h
namespace MyNamespace {
static const double GasConstant = 1.987;
class MyClass
{
// constructors, methods, etc.
};
}
我之前在 MyClass 声明中声明了 GasConstant(并且在源文件中有一个单独的定义,因为 C++ 不支持非整数类型的 const 初始化)。然而,我需要从其他文件访问它,而且从逻辑上讲,它似乎应该驻留在命名空间级别。
我的问题是,在这种情况下 static const
有什么作用?显然 const 意味着我无法为 GasConstant 分配新值,但是命名空间中的静态成员意味着什么。这是否类似于文件范围内的静态,其中成员在单元外部不可访问?
// MyClass.h
namespace MyNamespace {
static const double GasConstant = 1.987;
class MyClass
{
// constructors, methods, etc.
};
}
I previously had GasConstant declared within the MyClass declaration (and had a separate definition in the source file since C++ does not support const
initialization of non-integral types). I however need to access it from other files and also logically it seems like it should reside at the namespace level.
My questions is, what effect does static const
have in this case? Clearly const
means I can't assign a new value to GasConstant, but what does a static member at the namespace mean. Is this similar to static at file scope, where the member is not accessible outside of the unit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中,* 已弃用在命名空间范围
is中使用static
。它通常只能在源文件中看到,其作用是使变量成为该源文件的本地变量。也就是说,另一个源文件可以具有完全相同名称的变量,而不会发生冲突。在 C++ 中,使变量成为源文件本地变量的推荐方法是使用匿名命名空间。
我认为可以公平地说,代码标头中的
static
根本不正确。*正如汤姆在评论中指出的那样(以及在此答案 ),C++ 委员会推翻了在文件范围内弃用
static
使用的决定,因为这种用法始终是语言的一部分(例如,为了与 C 兼容)。The use of
static
at namespace scopeiswas* deprecated in C++. It would normally only ever be seen in a source file, where its effect is to make the variable local to that source file. That is, another source file can have a variable of exactly the same name with no conflict.In C++, the recommended way to make variables local to the source file is to use an anonymous namespace.
I think it's fair to say the
static
in the header in your code is simply incorrect.*As pointed out by Tom in the comments (and in this answer), the C++ committee reversed the decision to deprecate
static
use at file scope, on the basis that this usage will always be part of the language (e.g. for C compatibility).MSDN 说:
请记住,包含头文件意味着用头文件的实际代码替换“#include”指令。因此,静态变量仅在包含两个头文件的“.cpp”(已编译)文件中可见。
因此,每个“cpp”文件(包括标头)都将拥有自己的静态变量。
MSDN says:
Remember that including header files means to replace the "#include"-directive with the actual code of the header file. So, the static variables will be visible only in the ".cpp" (which is compiled) file that includes the two header files.
So each "cpp"-file including the headers will have it's own static variable.
如果这是一个头文件,则
static
在这种情况下不起作用。默认情况下,C++ 中的const
对象已经具有内部链接,因此无论您使用static
声明它还是不使用static
声明它都会导致没有任何区别。我假设您只是将声明从类移动到命名空间中。但是
static
在类声明的上下文中和在命名空间的上下文中具有完全不同的含义。在类内部,您需要static
。在命名空间中,static
是多余的。If this is a header file, then
static
has no effect in this case.const
objects already have internal linkage by default in C++, so whether you declare it withstatic
or withoutstatic
makes no difference whatsoever.I assume you simply moved the declaration from the class into the namespace. But
static
has totally different meaning in the context of the class declaration and in the context of namespace. Inside the class, you neededstatic
. In the namespace thestatic
is superfluous.