为什么在C ++中我们使用DWORD而不是unsigned int? [英] Why in C++ do we use DWORD rather than unsigned int?
问题描述
我不怕承认我有点是C ++新手,所以这可能是一个蠢的问题,但....
I'm not afraid to admit that I'm somewhat of a C++ newbie, so this might seem like a silly question but....
我看到DWORD在代码示例中使用了所有的地方。当我查找一个DWORD真正意味着什么,它显然只是一个unsigned int(0到4,294,967,295)。所以我的问题是,为什么我们有DWORD?它是什么给我们的整数类型unsigned int不?
I see DWORD used all over the place in code examples. When I look up what a DWORD truly means, its apparently just an unsigned int (0 to 4,294,967,295). So my question then is, why do we have DWORD? What does it give us that the integral type 'unsigned int' does not? Does it have something to do with portability and machine differences?
推荐答案
DWORD
不是C ++类型,它在< windows.h>
中定义。
DWORD
is not a C++ type, it's defined in <windows.h>
.
原因是 DWORD
具有Windows函数依赖的特定范围和格式,因此如果要求特定范围使用该类型。 (或者他们说当在罗马,做罗马人做。对于你,这恰好符合 unsigned int
,但可能不总是这样。为了安全,如果期望 DWORD
,不管它实际上是什么,请使用 DWORD
。
The reason is that DWORD
has a specific range and format Windows functions rely on, so if you require that specific range use that type. (Or as they say "When in Rome, do as the Romans do.") For you, that happens to correspond to unsigned int
, but that might not always be the case. To be safe, use DWORD
when a DWORD
is expected, regardless of what it may actually be.
例如,如果他们改变了 unsigned int
的范围或格式,他们可以使用不同的类型来 DWORD
的所有代码将是不明智的。 (同样,他们可以决定 DWORD
需要 unsigned long long
,更改它,所有代码使用 DWORD
将是不明智的。)
For example, if they ever changed the range or format of unsigned int
they could use a different type to underly DWORD
to keep the same requirements, and all code using DWORD
would be none-the-wiser. (Likewise, they could decide DWORD
needs to be unsigned long long
, change it, and all code using DWORD
would be none-the-wiser.)
c $ c> unsigned int 不需要 必须有范围0到4,294,967,295。请参见此处 a>。
Also note unsigned int
does not necessary have the range 0 to 4,294,967,295. See here.
这篇关于为什么在C ++中我们使用DWORD而不是unsigned int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!