基本的Delphi类型如何相互关联? [英] How are the basic Delphi types related to each other?

查看:171
本文介绍了基本的Delphi类型如何相互关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在Delphi 2007中,我发现这些声明(有些是冲突的,有些只是别名):



Types.pas:

  DWORD = LongWord; 
Largeint = Int64;

getmem.inc:

  DWORD =整数; 

Windows.pas:

  DWORD = Types.DWORD; 
SHORT = Smallint;
UINT = LongWord;
ULONG =红衣主教;
LONGLONG = Int64;
TLargeInteger = Int64;
ULONGLONG = UInt64;

这导致我认为基本签名的数字类型是SmallInt,Integer和Int64。无符号,有字节,WORD和UInt64。但是Cardinal和LongWord有什么区别? (顺便说一下,这些类型的原始和预期套件是什么?)



是否有一个类型为带符号的8位整数(Int8)?

  // Int8 =?unknown? 
UInt8 =字节;
Int16 = SmallInt;
UInt16 = Word;
Int32 =整数;
UInt32 = LongWord;
// Int64已经存在
// UInt64已经存在

最后,我应该定义Int和UInt,特别是关于C / C ++兼容性以及将来切换到其他平台(也可能是64位)? (相关问题当然是64位Delphi中如何定义各种数字类型?)

解决方案

签名的一字节整数类型为 ShortInt 。您可以记住它的大小,因为它不是与 short 类型的通常C实现相同的大小。



对于大写字母,首字母大写。 文档往往离开 int部分在小写字母中,如 ,但我认为更常见的是使用它。除非您使用平台SDK类型,否则不要在所有大写字母中写入类型,您希望您的代码显示其C根;否则我会写 Word DWord Long ULong 等。)



Delphi 2009(也许更早)已经定义了类似于 Int8 UInt32 。至于如何定义 Int UInt ,我会说不要。您使用的语言已经定义了整数 Cardinal ;不要引入新的类型名称。保留你已经拥有的名字,然后其他人都会知道你在说什么。 (此外, Int 已经是系统单元中的一个功能。)



使用 Cardinal 当你想要一个无符号的类型,不关心它的大小;当变量必须正好是四个字节时,使用 LongWord 同样对于整数 LongInt 使用 Cardinal 当你想要一个四字节的无符号类型;使用 LongWord ,当你想要一个通用的无符号类型,不关心大小。同样对于整数 LongInt ,现在。如果您正在编写16位代码,当您需要四个字节时,请使用 LongInt ,当您不要使用 Integer 不关心尺寸; Cardinal LongWord 不存在于Delphi和Turbo Pascal的16天之内。



多年来的常见智慧是,整数 Cardinal 将成为64位类型在64位编译器上,但显然不是这样。相反,他们会仍然是32位类型,就像他们在Microsoft C ++中的一样。此外,将有一个新的类型, NativeInt ,这将是64位编译器中的64位类型。 LongInt LongWord 类型将成为64位类型,因为它们的大小与指针类型,即使在16位时也是32位。


Delphi has long supported a few basic numeric types and I was wondering how they are related to each other.

In Delphi 2007 I found these declarations (some are conflicting, some are mere aliasses) :

Types.pas:

DWORD = LongWord;
Largeint = Int64;

getmem.inc:

DWORD = Integer;

Windows.pas:

DWORD = Types.DWORD;
SHORT = Smallint;
UINT = LongWord;
ULONG = Cardinal;
LONGLONG = Int64;
TLargeInteger = Int64;
ULONGLONG = UInt64;

This leads me into thinking the base signed numeric types are SmallInt, Integer and Int64. Unsigned, there's Byte, WORD and UInt64. But what is the difference between Cardinal and LongWord? (By the way, what's the original and intended casing for these types?)

And is there a type for signed 8 bit integers (Int8)?

// Int8 = ?unknown?;
UInt8 = Byte;
Int16 = SmallInt;
UInt16 = Word;
Int32 = Integer;
UInt32 = LongWord;
// Int64 already exists
// UInt64 already exists

Lastly, how should I define Int and UInt, especially with regard to C/C++ compatibility and a future switch to other platforms (possibly also 64 bit)? (A related question is, of course, how will the various numeric types be defined in 64-bit Delphi?)

解决方案

The signed one-byte integer type is ShortInt. You can remember its size by the fact that it's not the same size as usual C implementations of the short type.

As for capitalization, capitalize the first letter. The documentation tends to leave the "int" part at the end lowercase, as in Longint, but I think it's more common to capitalize it. Don't write the types in all capitals unless you're using Platform SDK types and you want your code to show its C roots; otherwise I'd just write Word and DWord, Long and ULong, etc.)

Delphi 2009, perhaps earlier, already defines types like Int8 and UInt32. As for how to define Int and UInt, I'd say don't. The language you're using already defines Integer and Cardinal; don't introduce new type names when you don't have to. Keep the names you already have, and then everyone else will know what you're talking about. (Besides, Int is already a function in the System unit.)

Use Cardinal when you want an unsigned type and don't care about its size; use LongWord when the variable must be exactly four bytes. Likewise for Integer and LongInt. Use Cardinal when you want a four-byte unsigned type; use LongWord when you want a generic unsigned type and don't care about the size. Likewise for Integer and LongInt, nowadays. If you're writing 16-bit code, use LongInt when you need four bytes and use Integer when you don't care about the size; Cardinal and LongWord didn't exist in Delphi's and Turbo Pascal's 16-bit days.

The common wisdom for years had been that Integer and Cardinal would become 64-bit types on a 64-bit compiler, but that is apparently not the case. Instead, they will remain 32-bit types, just as their counterparts in Microsoft C++ do. Furthermore, there will be a new type, NativeInt, which will be a 64-bit type in a 64-bit compiler. The LongInt and LongWord types will become 64-bit types because they have always been the same size as the Pointer type, which was 32 bits even in 16-bit times.

这篇关于基本的Delphi类型如何相互关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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