gpt4 book ai didi

c++ - 结构变量成员后的花括号是什么意思?

转载 作者:行者123 更新时间:2023-12-01 14:13:47 25 4
gpt4 key购买 nike

在一些维护(Valgrind'ing)期间,我遇到了这个代码:

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

我希望 {}告诉编译器始终将值初始化为 0(使用 new 或使用堆栈变量分配时),但我找不到任何示例或文档。我在这个假设中正确吗?如果不:

花括号有什么作用 {}后一个struct成员变量是什么意思?

最佳答案

这是 default member initializer (从 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 , bcvalue-initialized ( zero-initialized 对于内置类型)到 0 .

关于c++ - 结构变量成员后的花括号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61520931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com