结构/类成员的显式初始化 [英] Explicit initialization of struct/class members

查看:154
本文介绍了结构/类成员的显式初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct some_struct{
    int a;
};
some_struct n = {};

N.A为0后,这一点;

n.a will be 0 after this;

我知道这牙套初始化形式从C继承,并支持与C程序的兼容性,但这只能用C ++编译,不与C编译器。我使用Visual C ++ 2005。

I know this braces form of initialization is inherited from C and is supported for compatibility with C programs, but this only compiles with C++, not with the C compiler. I'm using Visual C++ 2005.

在C这种类型的初始化

struct some_struct n = {0};

是正确的,将零初始化一个结构的所有成员。

is correct and will zero-initialize all members of a structure.

是空对初始化标准括号的形式?我第一次看到这种形式的初始化在从MSDN一个WinAPI的教程。

Is the empty pair of braces form of initialization standard? I first saw this form of initialization in a WinAPI tutorial from msdn.

推荐答案

初始化的空括号的形式是在C ++标准(它是由语法明确允许)。见<一href=\"http://stackoverflow.com/questions/1352370/c-static-array-initialization-how-verbose-do-i-need-to-be/1352379#1352379\">http://stackoverflow.com/questions/1352370/c-static-array-initialization-how-verbose-do-i-need-to-be/1352379#1352379更多的细节,如果你有兴趣。

The empty braces form of initialization is standard in C++ (it's permitted explicitly by the grammar). See http://stackoverflow.com/questions/1352370/c-static-array-initialization-how-verbose-do-i-need-to-be/1352379#1352379 for more details if you're interested.

我假定它加入到C ++,因为对于要用于在所有情况下一个缺省初始化值的0值时,它可能不适合。

I assume that it was added to C++ because it might not be appropriate for a 0 value to be used for a default init value in all situations.

这篇关于结构/类成员的显式初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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