如何在 C++ 类的初始化列表中初始化成员结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4203010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize member-struct in initializer list of C++ class?
提问by Jan Rüegg
I have the following class definitions in c++:
我在 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 类的初始化程序中将“foo”结构(及其所有成员)初始化为零。可以这样做吗:
Bar::Bar()
: foo(),
x(8) {
}
... ?
……?
Or what exactly does the foo(x)do in the initializer list?
或者foo(x)在初始化列表中到底做了什么?
Or is the struct even initialized automatically to zero from the compiler?
或者结构甚至从编译器自动初始化为零?
回答by icecrime
First of all, you should (must !) read this c++ faqregarding POD and aggregates. In your case, Foo
is indeed a POD class and foo()
is a value initialization:
首先,您应该(必须!)阅读有关 POD 和聚合的C++ 常见问题解答。在您的情况下,Foo
确实是一个 POD 类并且foo()
是一个值初始化:
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
对 T 类型的对象进行值初始化意味着:
- 如果 T 是具有用户声明的构造函数 (12.1) 的类类型(第 9 条),则
调用 T的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化是格式错误的);- 如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都进行了值初始化;
- 如果 T 是数组类型,则每个元素都进行了值初始化;
- 否则,对象是零初始化的
So yes, foo will be zero-initialized. Note that if you removed this initialization from Bar
constructor, foo
would only be default-initialized:
所以是的, foo 将被零初始化。请注意,如果您从Bar
构造函数中删除此初始化,则foo
只会是默认初始化:
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;
如果没有为对象指定初始化程序,并且该对象是(可能是 cv 限定的)非 POD 类类型(或其数组),则该对象应默认初始化;如果对象是 const 限定类型,则基础类类型应具有用户声明的默认构造函数。 否则,如果没有为非静态对象指定初始化器,则该对象及其子对象(如果有)具有不确定的初始值;
回答by erjot
In standard C++ you need to make a ctor for Foo.
在标准 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;
};
In C++0x you may use uniform initialization list, but still you need dtor for Foo:
在 C++0x 中,您可以使用统一初始化列表,但仍然需要 dtor 用于 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.
要默认初始化foo
(作为Bar() : foo(), x(8) { }
),您需要给 Foo 一个默认的构造函数。