问题描述
我问这主要是出于好奇心。我不是一个真正的C(++)编码器,但为了拓宽我的地平线,我试图潜水在其他领域,现在,然后。当使用C ++和一些Winapi的东西做这件事时,我遇到了这个家伙:
I'm asking this mainly out of curiousity. I am not really a C(++) Coder but to widen my horizon I try to dive in other areas every now and then. When doing this with C++ and some Winapi things, I encountered this guy:
#if defined(MIDL_PASS)
typedef struct _LARGE_INTEGER {
#else // MIDL_PASS
typedef union _LARGE_INTEGER {
struct {
DWORD LowPart;
LONG HighPart;
};
struct {
DWORD LowPart;
LONG HighPart;
} u;
#endif //MIDL_PASS
LONGLONG QuadPart;
} LARGE_INTEGER;
所以,我看到它,根据MIDL_PASS是否设置,这是一个非常
So, the way I see it, depending on MIDL_PASS being set or not, this is either a very compact struct with only a LONGLONG in it, or the much more interesting case, this becomes a union.
如果这是一个联合,对我来说还是有意义的,有两个可能的访问,一次在一个块中的LONGLONG,并且一次结构与低和Highpart。
到目前为止很好。
In case this is a union, it still makes sense to me, to have two possibilites of access, once the LONGLONG in one chunk, and once the struct with Low and Highpart.So far so good.
但是我不能意识到结构被声明两次,相同。看来他们都是匿名的,但后者可以通过u。
But I cannot make any sense out of the fact that the struct is declared twice, identically. It seems they are both anonymous, but the latter one is available via "u".
现在到我的问题:
为什么两个结构体被定义(冗余?),第一个结构体的目的是什么,如果我不能访问它,由于没有绑定到任何类型/变量名。
Why are the two structs defined (redundantly?), what is the purpose of the first one, if i cannot even access it, due to not being bound to any type / variable name.
期待您的回答:)
问候,
汤姆
推荐答案
Microsoft提供匿名结构作为(它们的示例显示了另一个结构体中的一个结构体,但是结合体中的结构体类似)。如果你不介意基于他们的扩展的非可移植代码,你可以使用像:
Microsoft provides anonymous structs as an extension (their example shows one struct inside another struct, but a struct in a union is similar). If you don't mind non-portable code based on their extension, you can use things like:
LARGE_INTEGER a;
a.LowPart = 1;
但如果您想要可移植代码,您需要:
but if you want portable code, you need:
a.u.LowPart = 1;
联盟允许您使用。
这篇关于无法理解LARGE_INTEGER结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!