为什么不将C ++结构初始化为`= {0}`并将其所有成员都设置为0? [英] Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?

查看:196
本文介绍了为什么不将C ++结构初始化为`= {0}`并将其所有成员都设置为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量测试并编写这个答案(注意:对它的否决是在我全部重写之前),我不明白为什么= {0}不能将结构的所有成员都设置为零!

After doing a ton of testing and writing this answer (note: the downvote on it was BEFORE my total rewrite of it), I can't understand why = {0} doesn't set all members of the struct to zero!

如果您这样做:

struct data_t
{
    int num1 = 100;
    int num2 = -100;
    int num3;
    int num4 = 150;
};

data_t d3 = {0};
printf("d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n",
       d3.num1, d3.num2, d3.num3, d3.num4);

...输出为:

d3.num1 = 0
d3.num2 = -100
d3.num3 = 0
d3.num4 = 150

...虽然我期望的输出是这样的:

...although I expected the output to be this:

d3.num1 = 0
d3.num2 = 0
d3.num3 = 0
d3.num4 = 0

...这意味着只有FIRST成员设置为零,其余所有成员均设置为默认值.

...which means that only the FIRST member was set to zero, and all the rest were set to their defaults.

我总是给人一种印象,用这三种方式中的任何一种初始化一个结构都会对其进行零初始化,但是显然我错了!

I was always under the impression that initializing a struct in any of these 3 ways would zero-initialize it, but obviously I'm wrong!

  1. data_t d{}
  2. data_t d = {}
  3. data_t d = {0}
  1. data_t d{}
  2. data_t d = {}
  3. data_t d = {0}

我的主要收获来自此答案因此是这样:

My key takeaway from this answer is therefore this:

这里最重要的是,data_t d{}data_t d = {}data_t d = {0}中的任何一个实际上都没有将结构的所有成员设置为零!

The big take-away here is that NONE of these: data_t d{}, data_t d = {}, and data_t d = {0}, actually set all members of a struct to zero!

  1. data_t d{}将所有值设置为结构中定义的默认值.
  2. data_t d = {}还将所有值设置为其默认值.
  3. 并且data_t d = {0}仅将FIRST值设置为零,并将所有其他值设置为其默认值.
  1. data_t d{} sets all values to their defaults defined in the struct.
  2. data_t d = {} also sets all values to their defaults.
  3. And data_t d = {0} sets only the FIRST value to zero, and all other values to their defaults.

那么,为什么不将C ++结构初始化为= {0}并将其所有成员都设置为0?

So, why doesn't initializing a C++ struct to = {0} set all of its members to 0?

请注意,我上面的主要要点实际上与我多年来使用的这份看起来相当正式的文档相矛盾( https://en.cppreference.com/w/cpp/language/zero_initialization ),它表示T t = {} ;T {} ;都是零初始值设定项,实际上,根据根据我的测试和上面的内容,不是.

Note that my key take-aways above actually contradict this rather official-looking documentation I've been using for years (https://en.cppreference.com/w/cpp/language/zero_initialization), which says that T t = {} ; and T {} ; are both zero initializers, when in fact, according to my tests and take-away above, they are NOT.

  1. 如何将结构初始化为0在C ++中
  2. 更新:我也刚刚指向此参考文献:做什么{0}是什么意思初始化对象?
  1. How to initialize a struct to 0 in C++
  2. Update: I was just pointed to this reference too: What does {0} mean when initializing an object?

推荐答案

那么,为什么不将C ++结构初始化为= {0}并将其所有成员都设置为0?

So, why doesn't initializing a C++ struct to = {0} set all of its members to 0?

因为您只提供一个值,而该类有一个以上的成员.

Because you are only providing one value, while the class has more than one member.

当您拥有T t{};T t = {}时,所做的事情称为> 值初始化 .在值初始化中,如果对象/成员没有默认构造函数或默认成员初始化程序,则编译器将初始化为objec/member降为零.因此,

When you have T t{}; or T t = {} what you are doing is called value initialization. In value initialization, if the object/member does not have a default constructor, or a default member initializer, then the compiler falls back to zero initializing the objec/member. So with

data_t d{}

成员的值顺序为100,-100、0、150,并且num30发生是因为它没有默认值,并且您没有在{}中提供值,所以编译器降为零,初始化num3.这与data_t d = {}相同.使用data_t d = {0}时,您提供了第一个元素,因此num10,但是与前两个元素一样,所有其他成员(如果有一个)都使用其默认值初始化,如果没有,则初始化为零. ,为您提供0,-100、0、150的成员值.

the value of the members in order would be 100, -100, 0 ,150 and that 0 for num3 happens because it has no default and you did not provide a value in the {} so the compiler falls back to zero initializing num3. This is the same with data_t d = {}. With data_t d = {0} you provide the first element, so num1 is 0, but then like the first two, all of the other members are initialized with their default value if they have one, or zero initialized if they don't, giving you 0, -100, 0, 150 for the member values.

这是C ++ 11发布并允许默认成员初始化程序时发生的更改.

This was a change that happened when C++11 was released and allowed for default member initializers.

如果您的data_t被定义为

typedef struct
{
    int num1;
    int num2;
    int num3;
    int num4;
} data_t;

然后data_t d{}data_t d = {}data_t d = {0}都将为您提供零初始化类,因为没有默认的成员初始值设定项,而您在
braced-init-list ({...}的技术名称)为零,因此所有成员均变为零.

then data_t d{}, data_t d = {}, data_t d = {0} would all leave you with a zero initialized class since there are no default member initializers and the only value you provide in you braced-init-list (the technical name for {...}) is zero so all members become zero.

这篇关于为什么不将C ++结构初始化为`= {0}`并将其所有成员都设置为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆