结构变量成员后的花括号是什么意思? [英] What do curly braces after a struct variable member mean?

查看:82
本文介绍了结构变量成员后的花括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行一些维护(Valgrind'ing)过程中,我遇到了以下代码:

During some maintenance (Valgrind'ing) I came across this code:

#pragma pack(push, 1)
struct somename
{
  uint16_t a{};
  uint16_t b{};
  uint32_t  c{};
};
#pragma pack(pop)

我希望 {} 告诉编译器始终将值初始化为0(使用new或使用stack变量进行分配时),但是我找不到关于它的任何示例或文档.我在这个假设中正确吗?如果没有:

I would expect that the {} tell the compiler to always initialize the values to 0 (when allocating using new, or using a stack variable), but I cannot find any examples or documentation on that. Am I correct in this assumption? If not:

结构成员变量后的花括号 {} 是什么意思?

What do the curly braces {} after a struct member variable mean?

推荐答案

这是默认成员初始值设定项(自C ++ 11起).

This is default member initializer (since C++11).

(重点是我的)

通过默认的成员初始化程序,是成员声明中包含的大括号或等于初始值设定项,如果从构造函数的成员初始化程序列表中省略了该成员,则使用该变量.

Through a default member initializer, which is a brace or equals initializer included in the member declaration and is used if the member is omitted from the member initializer list of a constructor.

如果成员具有默认成员初始化程序,并且该成员也出现在构造函数的成员初始化列表中,则该构造函数将忽略默认成员初始化程序.

If a member has a default member initializer and also appears in the member initialization list in a constructor, the default member initializer is ignored for that constructor.

实际上,数据成员 a b c

As the effect, the data members a, b and c are value-initialized (zero-initialized for built-in types) to 0.

这篇关于结构变量成员后的花括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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