static const 对命名空间成员有什么影响

发布于 2024-09-02 14:52:43 字数 427 浏览 2 评论 0原文

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

复古式 2024-09-09 14:52:43

在 C++ 中,* 已弃用在命名空间范围 is 中使用 static。它通常只能在源文件中看到,其作用是使变量成为该源文件的本地变量。也就是说,另一个源文件可以具有完全相同名称的变量,而不会发生冲突。

在 C++ 中,使变量成为源文件本地变量的推荐方法是使用匿名命名空间。

我认为可以公平地说,代码标头中的 static 根本不正确。

*正如汤姆在评论中指出的那样(以及在此答案 ),C++ 委员会推翻了在文件范围内弃用 static 使用的决定,因为这种用法始终是语言的一部分(例如,为了与 C 兼容)。

The use of static at namespace scope is was* 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).

究竟谁懂我的在乎 2024-09-09 14:52:43

MSDN 说:

当修改变量时,static关键字指定
变量具有静态持续时间(它是
程序开始时分配,并且
程序结束时释放)和
将其初始化为 0 除非另一个
值已指定。当修改一个
文件范围内的变量或函数,
static 关键字指定
变量或函数具有内部
链接(其名称在
在它所在的文件之外
声明)。

请记住,包含头文件意味着用头文件的实际代码替换“#include”指令。因此,静态变量仅在包含两个头文件的“.cpp”(已编译)文件中可见。

因此,每个“cpp”文件(包括标头)都将拥有自己的静态变量。

MSDN says:

When modifying a variable, the static keyword specifies that the
variable has static duration (it is
allocated when the program begins and
deallocated when the program ends) and
initializes it to 0 unless another
value is specified. When modifying a
variable or function at file scope,
the static keyword specifies that the
variable or function has internal
linkage (its name is not visible from
outside the file in which it is
declared).

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.

陌路黄昏 2024-09-09 14:52:43

如果这是一个头文件,则 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 with static or without static 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 needed static. In the namespace the static is superfluous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文