无名结构/联合 [英] nameless struct / union

查看:171
本文介绍了无名结构/联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我讨厌在两个单词之间使用带有点的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 Parkoff

I hate using struct / union with dot between two words. How can I use
one word instead of two words because I want the source code look reading
clear. three variables are shared inside one variable. I manipulate to
change 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

推荐答案

Bryan Parkoff写道:
Bryan Parkoff wrote:

我讨厌在两个单词之间使用带有点的struct / union。我怎么能用
使用一个单词而不是两个单词,因为我希望源代码看起来很清楚。三个变量在一个变量中共享。我将
操作来更改8位数据,然后才能更改16位
数据和32位数据。例如。


union


{


struct _Byte

{


U_BYTE AAL;


U_BYTE AAH;


}字节;


struct _Word


{


U_WORD AAW;

} Word;


struct _DWORD

{


U_DWORD AA;


} DWord;


};


int main()
< br $>
{


//我讨厌两个字之间的点。


Byte.AAL = 0xFF;


Byte.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;
I hate using struct / union with dot between two words. How can I
use one word instead of two words because I want the source code look
reading clear. three variables are shared inside one variable. I
manipulate to change 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;



呃......我对未命名的工会有点生疏。一个未命名的工会

是否创建了一个全局实例?


另外,你使用首先分配工会的一部分然后

使用另一部分有不确定的行为,IIRC。


V

-

请删除资本''A'当通过电子邮件回复

我没有回复最热门的回复,请不要问

Uh... I''m a bit rusty on unnamed unions. Does an unnamed union
create a global instance?

Also, your use of first assigning one part of the union and then
using another part has undefined behaviour, IIRC.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

呃......我对未命名的工会有点生疏。一个未命名的联盟

是否创建了一个全局实例?
Uh... I''m a bit rusty on unnamed unions. Does an unnamed union
create a global instance?



我甚至不知道它是否允许在结构之外。它实际上是吗?

I didn''t even know it''s allowed outside a struct. Is it actually?


另外,你使用第一个分配联合的一部分然后使用另一部分使用
具有未定义的行为,IIRC 。
Also, your use of first assigning one part of the union and then
using another part has undefined behaviour, IIRC.



是的。您必须始终只读取您上次写入的成员,否则

行为未定义。

Yes. You must always only read the member you last wrote to, otherwise the
behavior is undefined.


12月11日,5日:49 am,Bryan Parkoff < nos ... @nospam.comwrote:
On Dec 11, 5:49 am, "Bryan Parkoff" <nos...@nospam.comwrote:

我讨厌在两个单词之间使用带有点的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;


};
I hate using struct / union with dot between two words. How can I use
one word instead of two words because I want the source code look reading
clear. three variables are shared inside one variable. I manipulate to
change 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;

};



对不起,它没有回复你原来的帖子(其他人似乎已经做了

),但使用了类型名称以下划线开头

后面跟一个大写字母不允许使用C ++

标准(除非你的代码是编译器实现的一部分

本身)。一个以下划线开头且后面跟着一个

大写字母的名字不能在全局命名空间中使用,但

可能在其他地方(但我建议不要它要避免

混乱)。有关详细信息,请参阅标准的第17.4.3.1.2节。


-

计算建模,CSIRO(CMIS)

澳大利亚墨尔本

Sorry, it''s not answering your original post (others seem to be doing
that already), but using a type name which starts with an underscore
and is followed by an uppercase letter is not allowed by the C++
standard (unless your code is part of the compiler implementation
itself). A name starting with an underscore and NOT followed by an
uppercase letter cannot be used in the global namespace, but
presumably could be elsewhere (but I''d recommend against it to avoid
confusion). See section 17.4.3.1.2 of the standard for details.

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia


这篇关于无名结构/联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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