问题描述
在进行一些维护(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).
(重点是我的)
如果成员具有默认成员初始化程序,并且该成员也出现在构造函数的成员初始化列表中,则该构造函数将忽略默认成员初始化程序.
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
是值已初始化(对内置类型进行零初始化) 0
.
As the effect, the data members a
, b
and c
are value-initialized (zero-initialized for built-in types) to 0
.
这篇关于结构变量成员后的花括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!