如何在 C++ 的初始化列表中初始化成员结构班级?

发布于 2024-10-02 17:18:28 字数 349 浏览 5 评论 0原文

我在 C++ 中有以下类定义:

struct Foo {
  int x;
  char array[24];
  short* y;
};

class Bar {
  Bar();

  int x;
  Foo foo;
};

并且想在 Bar 类的初始化程序中将“foo”结构(及其所有成员)初始化为零。可以这样做吗:

Bar::Bar()
  : foo(),
    x(8) {
}

...?

或者 foo(x) 在初始化列表中到底做了什么?

或者该结构甚至从编译器自动初始化为零?

I have the following class definitions in c++:

struct Foo {
  int x;
  char array[24];
  short* y;
};

class Bar {
  Bar();

  int x;
  Foo foo;
};

and would like to initialize the "foo" struct (with all its members) to zero in the initializer of the Bar class. Can this be done this way:

Bar::Bar()
  : foo(),
    x(8) {
}

... ?

Or what exactly does the foo(x) do in the initializer list?

Or is the struct even initialized automatically to zero from the compiler?

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

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

发布评论

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

评论(2

叹倦 2024-10-09 17:18:28

首先,您应该(必须!)阅读此

对类型的对象进行值初始化
T的意思是:

  • 如果 T 是具有用户声明构造函数 (12.1) 的类类型(第 9 条),则使用默认构造函数
    for T 被调用(如果 T 没有可访问的默认构造函数,则初始化是错误的);
  • 如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都会进行值初始化;
  • 如果 T 是数组类型,则每个元素都进行值初始化;
  • 否则,对象将被零初始化

所以,是的, foo 将被零初始化。请注意,如果您从 Bar 构造函数中删除此初始化,则 foo 将仅是 default-initialized

如果没有指定初始值设定项
对象,并且该对象是(可能
cv 限定的)非 POD 类类型(或
数组),该对象应是
默认初始化;如果该物体是
const 限定类型,
底层类类型应具有
用户声明的默认构造函数。
否则,如果没有初始化程序
为非静态对象指定时,
对象及其子对象(如果有),
有一个不确定的初始值
;

First of all, you should (must !) read this c++ faq regarding POD and aggregates. In your case, Foo is indeed a POD class and foo() is a value initialization :

To value-initialize an object of type
T means:

  • if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor
    for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized

So yes, foo will be zero-initialized. Note that if you removed this initialization from Bar constructor, foo would only be default-initialized :

If no initializer is specified for an
object, and the object is of (possibly
cv-qualified) non-POD class type (or
array thereof), the object shall be
default-initialized; if the object is
of const-qualified type, the
underlying class type shall have a
user-declared default constructor.
Otherwise, if no initializer is
specified for a nonstatic object, the
object and its subobjects, if any,
have an indeterminate initial
value
;

刘备忘录 2024-10-09 17:18:28

在标准 C++ 中,您需要为 Foo 创建一个 ctor。

struct Foo {

  Foo(int const a, std::initializer_list<char> const b, short* c)
    : x(a), y(c) {
    assert(b.size() >= 24, "err");
    std::copy(b.begin(), b.begin() + 24, array);
  }

  ~Foo() { delete y; }

  int x;
  char array[24];
  short* y;
};

class Bar {

  Bar() : x(5), foo(5, {'a', 'b', ..., 'y', 'z'},
    new short(5)) { }

  private:

  int x;
  Foo foo;
};

在 C++0x 中,您可以使用统一初始化列表,但仍然需要 Foo 的 dtor:

class Bar {

  Bar() : x(5), foo{5, new char[24]{'a', 'b', ..., 'y', 'z'},
    new short(5)} { }
  ~Bar() { delete[] foo.array; delete foo.y;}
  }
  private:

  int x;
  Foo foo;
};

默认初始化 foo (如 Bar() : foo(), x(8) { })你需要给 Foo 一个默认的 ctor。

In standard C++ you need to make a ctor for Foo.

struct Foo {

  Foo(int const a, std::initializer_list<char> const b, short* c)
    : x(a), y(c) {
    assert(b.size() >= 24, "err");
    std::copy(b.begin(), b.begin() + 24, array);
  }

  ~Foo() { delete y; }

  int x;
  char array[24];
  short* y;
};

class Bar {

  Bar() : x(5), foo(5, {'a', 'b', ..., 'y', 'z'},
    new short(5)) { }

  private:

  int x;
  Foo foo;
};

In C++0x you may use uniform initialization list, but still you need dtor for Foo:

class Bar {

  Bar() : x(5), foo{5, new char[24]{'a', 'b', ..., 'y', 'z'},
    new short(5)} { }
  ~Bar() { delete[] foo.array; delete foo.y;}
  }
  private:

  int x;
  Foo foo;
};

To default initialize foo (as Bar() : foo(), x(8) { }) you need to give Foo a default ctor.

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