初始化类的静态常量非整数数据成员

发布于 2024-12-21 08:29:38 字数 440 浏览 1 评论 0原文

考虑下面的示例程序:

#include <iostream>

using namespace std;

class test
{
   public:
      static const float data;
};

float const test::data = 10;   // Line1


int main()
{
   cout << test::data;
   cout << "\n";

   return 0;
}

请注意示例代码中的注释 Line1

问题:

  1. Line1 是否正在执行日期成员data 的初始化?
  2. Line1 是初始化静态常量非整数数据成员的唯一方法吗?

Consider the sample program below:

#include <iostream>

using namespace std;

class test
{
   public:
      static const float data;
};

float const test::data = 10;   // Line1


int main()
{
   cout << test::data;
   cout << "\n";

   return 0;
}

Please note the comment Line1 in the sample code.

Questions:

  1. Is Line1 doing the initialization of the date member data?
  2. Is Line1 the only way to initialize a static const non-integral data member?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

a√萤火虫的光℡ 2024-12-28 08:29:38

Line1是否正在初始化日期成员数据?

当然是这样,并且提供了对象的定义。请注意,这只能在单个翻译单元中完成,因此如果类定义位于头文件中,那么它应该位于源文件中。

Line1 是初始化静态常量非整数数据成员的唯一方法吗?

在 C++03 中确实如此。在 C++11 中,const 文字类型的任何静态成员都可以在类定义中具有初始化程序。如果该成员是“odr-used”(粗略地说,如果您执行任何需要其地址而不仅仅是其值的操作),您仍然需要该成员的定义。在这种情况下,定义再次需要位于单个翻译单元中,并且不得具有初始值设定项(因为类定义中已经有一个初始值设定项)。

Is Line1 doing the initialization of the date member data?

It certainly is, as well as providing the definition of the object. Note that this can only be done in a single translation unit, so if the class definition is in a header file, then this should be in a source file.

Is Line1 the only way to initialize a static const non-integral data member?

In C++03 it was. In C++11, any static member of const literal type can have an initialiser in the class definition. You still need a definition of the member if it's "odr-used" (roughly speaking, if you do anything that needs its address, not just its value). In this case, the definition again needs to be in a single translation unit, and must not have an initialiser (since there's already one in the class definition).

夕色琉璃 2024-12-28 08:29:38

在现代 C++ 中,您可以内联初始化任何常量表达式。这需要更改语法:

class test
{
   public:
      static constexpr float data = 10.0f;
};

float constexpr test::data;

In contemporary C++ you can initialize any constant expression inline. This requires a change of syntax:

class test
{
   public:
      static constexpr float data = 10.0f;
};

float constexpr test::data;
晨光如昨 2024-12-28 08:29:38
  1. 第 1 行定义静态数据成员data,其中包括设置其值。
  2. 对于非整数类型的静态数据成员,成员定义确实是唯一设置值的地方。对于整数、长整型、枚举等,您可以将值放入声明中。您仍然必须包含定义,但在这种情况下,您不得输入任何值。

编辑:正如 Mike Seymor 指出的那样,#2 已经过时了。根据新的 C++11 标准,1998 年和 C++03 标准仅为整型类型保留的替代语法已扩展到所有常量,无论其类型如何。

  1. Line1 does definition of the static data member data, which includes setting its value.
  2. For static data members of non-integral types, member definition is indeed the only place to set a value. For integers, longs, enums, etc. you can put the value in with the declaration. You must still include a definition, but in that case you must not put in any value.

EDIT: As Mike Seymor pointed out, the #2 is out of date. According to the new C++11 standard, the alternative syntax that was reserved only for integral types by the 1998 and C++03 standards has been extended to all constants, regardless of their type.

你的笑 2024-12-28 08:29:38
  1. 是的。

2.

在C++11中,你可以说

class test {
public:
    constexpr static float data = 10.0; // data is implicitly const
};

在C++03中,是的。

  1. Yes.

2.

In C++11, you can say

class test {
public:
    constexpr static float data = 10.0; // data is implicitly const
};

In C++03, it's Yes.

蓝眼泪 2024-12-28 08:29:38

Line1是否正在初始化日期成员数据?

是的。

Line1 是初始化静态常量非整数数据的唯一方法吗
会员?

是的。

Is Line1 doing the initialization of the date member data?

Yes.

Is Line1 the only way to initialize a static const non-integral data
member?

Yes.

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