简体   繁体   English

作为结构初始化器,{}和{0}之间有什么区别?

[英]What is the difference between {} and {0} as struct initialisers?

Is there any difference between this: 这有什么区别:

struct something s = {};

And this? 还有这个?

struct something s = {0};

From all I know, both initialise every member to zero. 据我所知,两者都将每个成员初始化为零。

struct something s = {}; isn't valid C (unless they added it in C11) , but is valid C++. 是无效的C (除非他们在C11中添加它) ,但它是有效的C ++。 GCC seems to allow it in C programs as an extension (though I don't see it mentioned in the docs on http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html , but I might just be missing it). GCC似乎允许它在C程序中作为扩展(虽然我没有在http://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html上的文档中看到它,但我可能只是遗漏了它)。

In C++ it will result in 'value-initialization', which basically means that the default constructor is called for each member (zero-initialization for non-class members). 在C ++中,它将导致'值初始化',这基本上意味着为每个成员调用默认构造函数(非类成员的零初始化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM