类字段如何初始化?

发布于 2024-09-09 23:47:09 字数 634 浏览 8 评论 0原文

这是一个基本问题,但我很难找到明确的答案。

除了方法中的赋值之外,初始化器是否列出了 C++ 中初始化类字段的唯一方法?

如果我使用了错误的术语,这就是我的意思:

class Test
{
public:
    Test(): MyField(47) { }  // acceptable
    int MyField;
};

class Test
{
public:
    int MyField = 47; // invalid: only static const integral data members allowed
};

编辑:特别是,是否有一种很好的方法来使用结构初始值设定项来初始化结构字段?例如:

struct MyStruct { int Number, const char* Text };

MyStruct struct1 = {};  // acceptable: zeroed
MyStruct struct2 = { 47, "Blah" } // acceptable

class MyClass
{
    MyStruct struct3 = ???  // not acceptable
};

A bit of a basic question, but I'm having difficulty tracking down a definitive answer.

Are initializer lists the only way to initialize class fields in C++, apart from assignment in methods?

In case I'm using the wrong terminology, here's what I mean:

class Test
{
public:
    Test(): MyField(47) { }  // acceptable
    int MyField;
};

class Test
{
public:
    int MyField = 47; // invalid: only static const integral data members allowed
};

EDIT: in particular, is there a nice way to initialize a struct field with a struct initializer? For example:

struct MyStruct { int Number, const char* Text };

MyStruct struct1 = {};  // acceptable: zeroed
MyStruct struct2 = { 47, "Blah" } // acceptable

class MyClass
{
    MyStruct struct3 = ???  // not acceptable
};

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

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

发布评论

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

评论(4

雨轻弹 2024-09-16 23:47:09

在 C++x0 中,第二种方法也应该有效。

初始化列表是 C++ 中初始化类字段的唯一方法吗?

就您的编译器而言:是的。

In C++x0 the second way should work also.

Are initializer lists the only way to initialize class fields in C++?

In your case with your compiler: Yes.

乙白 2024-09-16 23:47:09

静态成员可以以不同的方式初始化:

class Test {
    ....
    static int x;
};

int Test::x = 5;

我不知道你是否称其为“好”,但你可以相当干净地初始化结构成员,如下所示:

struct stype {
const char *str;
int val;
};

stype initialSVal = {
"hi",
7
};

class Test {
public:
    Test(): s(initialSVal) {}
    stype s;
};

Static members can be initialised differently:

class Test {
    ....
    static int x;
};

int Test::x = 5;

I don't know if you call this 'nice', but you can initialise struct members fairly cleanly like so:

struct stype {
const char *str;
int val;
};

stype initialSVal = {
"hi",
7
};

class Test {
public:
    Test(): s(initialSVal) {}
    stype s;
};
守护在此方 2024-09-16 23:47:09

只是提一下,在某些情况下,您别无选择,只能使用初始值设定项列表在构造时设置成员的值:

class A
{
 private:

  int b;
  const int c;

 public:

 A() :
  b(1),
  c(1)
 {
  // Here you could also do:
  b = 1; // This would be a reassignation, not an initialization.
        // But not:
  c = 1; // You can't : c is a const member.
 }
};

Just to mention that in some cases, you have no choice but to use initializer lists to set a member's value on construction:

class A
{
 private:

  int b;
  const int c;

 public:

 A() :
  b(1),
  c(1)
 {
  // Here you could also do:
  b = 1; // This would be a reassignation, not an initialization.
        // But not:
  c = 1; // You can't : c is a const member.
 }
};
我不在是我 2024-09-16 23:47:09

推荐和首选的方法是初始化构造函数中的所有字段,就像第一个示例一样。这对于结构也有效。请参阅此处:在类中初始化 static struct tm

The recommended and preferred way is to initialize all fields in the constructor, exactly like in your first example. This is valid also for structs. See here: Initializing static struct tm in a class

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