问题描述
我目前正在与传统的 C ++工作的code,成功地用gcc 2.9.X.结果编译
我一直在问到端口这份遗产code到GCC 3.4.X.大多数错误很容易被纠正,但这个特殊的方式让我为难。
I'm currently working with legacy C++ code, successfully compiled with gcc 2.9.X.
I've been asked to port this legacy code to gcc 3.4.X. Most of the errors were easily corrected, but this particular one puzzles me.
上下文:
struct TMessage
{
THeader header;
TData data;
};
struct THeader
{
TEnum myEnum;
TBool validity;
};
做了什么:
const TMessage init = {{0}};
/* Later in the code ... */
TMessage message = init;
我的问题(S):结果
什么是{{}}运营商的意义?第一个字段(头的),它初始化为二进制0?第一个结构的第一个字段(枚举的),它初始化为(字面)0?
My question(s) :
What is the meaning of the {{}} operator ? Does it initialize the first field (the header) to a binary 0 ? Does it initialize the first field of the first structure (the enum) to (literal) 0 ?
3.4.6中的错误,我得到的是从'诠释'到'TEnum
,无论是与一个或两对大括号的无效转换。
The 3.4.6 error I get is invalid conversion from 'int' to 'TEnum'
, either with one or two pairs of curly brackets.
如何将我的结构中设置了一堆0的没有用memset?
How can I set my structure to a bunch of 0's without using memset ?
在此先感谢。
推荐答案
据初始化的POD结构的所有字段为0。
It initialises all fields of the POD structure to 0.
理由:
const SomeStruct init = {Value};
初始化SomeStruct到价值,结构,零剩下的第一个字段(我忘了在标准的部分,但它的存在的地方)
Initialises the first field of SomeStruct to Value, the rest of the structure to zero (I forget the section in the standard, but it's there somewhere)
因此:
const SomeOtherStruct init = {{Value}};
初始化该结构的第一场的第一场(其中结构的第一场是本身一POD结构)到值,第一场的零的其余部分,并且该结构的为0的其余部分。
Initialises the first field of the first field of the structure (where the first field of the structure is itself a POD struct) to Value, and the rest of the first field to zero, and the rest of the structure to 0.
此外,这不仅是工作,因为C ++禁止 INT
隐式转换为枚举类型,所以你可以这样做:
Additionally, this only isn't working because c++ forbids implicit conversion of int
to enum types, so you could do:
const SomeOtherStruct init = {{TEnum(0)}};
这篇关于什么是双大括号初始化C-结构的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!