本文介绍了无名结构/联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我讨厌在两个单词之间使用带有点的struct / union。我如何使用 一个单词而不是两个单词,因为我希望源代码看起来很清楚 clear。三个变量在一个变量中共享。在操作改变16位数据和32位数据之前,我操纵了更改8位数据。 例如。 union { struct _Byte { U_BYTE AAL; U_BYTE AAH; }字节; struct _Word { U_WORD AAW; } Word; struct _DWORD { U_DWORD AA; } DWord; }; int main() { //我讨厌两个单词之间的点。 Byte.AAL = 0xFF; 字节。 AAH = 0x20; Byte.AAL + = 0x0A; Byte.AAH + = 0x01; Word.AAW + = 0xFF; DWord.AA + = 0xFFFF; //很容易在里面读取变量struct / union。 AAL = 0xFF; AAH = 0x20; AAL + = 0x0A; AAH + = 0x01; AAW + = 0xFF; AA + = 0xFFFF; - Bryan ParkoffI hate using struct / union with dot between two words. How can I useone word instead of two words because I want the source code look readingclear. three variables are shared inside one variable. I manipulate tochange 8-bit data before it causes to change 16-bit data and 32-bit data.For example.union{struct _Byte{U_BYTE AAL;U_BYTE AAH;} Byte;struct _Word{U_WORD AAW;} Word;struct _DWORD{U_DWORD AA;} DWord;};int main(){// I hate dot between 2 words.Byte.AAL = 0xFF;Byte.AAH = 0x20;Byte.AAL += 0x0A;Byte.AAH += 0x01;Word.AAW += 0xFF;DWord.AA += 0xFFFF;// It is easy reading variable inside struct / union.AAL = 0xFF;AAH = 0x20;AAL += 0x0A;AAH += 0x01;AAW += 0xFF;AA += 0xFFFF;--Bryan Parkoff推荐答案 呃......我对未命名的工会有点生疏。一个未命名的工会 是否创建了一个全局实例? 另外,你使用首先分配工会的一部分然后 使用另一部分有不确定的行为,IIRC。 V - 请删除资本''A'当通过电子邮件回复 我没有回复最热门的回复,请不要问Uh... I''m a bit rusty on unnamed unions. Does an unnamed unioncreate a global instance?Also, your use of first assigning one part of the union and thenusing another part has undefined behaviour, IIRC.V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 我甚至不知道它是否允许在结构之外。它实际上是吗?I didn''t even know it''s allowed outside a struct. Is it actually? 是的。您必须始终只读取您上次写入的成员,否则 行为未定义。Yes. You must always only read the member you last wrote to, otherwise thebehavior is undefined. 对不起,它没有回复你原来的帖子(其他人似乎已经做了 ),但使用了类型名称以下划线开头 后面跟一个大写字母不允许使用C ++ 标准(除非你的代码是编译器实现的一部分 本身)。一个以下划线开头且后面跟着一个 大写字母的名字不能在全局命名空间中使用,但 可能在其他地方(但我建议不要它要避免 混乱)。有关详细信息,请参阅标准的第17.4.3.1.2节。 - 计算建模,CSIRO(CMIS) 澳大利亚墨尔本Sorry, it''s not answering your original post (others seem to be doingthat already), but using a type name which starts with an underscoreand is followed by an uppercase letter is not allowed by the C++standard (unless your code is part of the compiler implementationitself). A name starting with an underscore and NOT followed by anuppercase letter cannot be used in the global namespace, butpresumably could be elsewhere (but I''d recommend against it to avoidconfusion). See section 17.4.3.1.2 of the standard for details.--Computational Modeling, CSIRO (CMIS)Melbourne, Australia 这篇关于无名结构/联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!