DELPHI设置枚举类型size
?
delphi枚舉類型長度默認為2個字節(jié)(單字),而在C中枚舉為4個字節(jié)(雙字),如果需要跨這兩個平臺編程,傳輸結構時會由于數(shù)據(jù)長度不一造成災難。
經(jīng)過查找資料,原來delphi可以通過{$Z+} {$Z-} {$Z1} {$Z4} 等宏設置枚舉類型的長度,小至1個字節(jié),大至4個字節(jié)。
官方說明如下:
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsminimumenumerationsize_xml.html
The $Z directive controls the minimum storage size of Delphi enumerated types.?
An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values, and if the type was declared in the {$Z1} state (the default). If an enumerated type has more than 256 values, or if the type was declared in the {$Z2} state, it is stored as an unsigned word. Finally, if an enumerated type is declared in the {$Z4} state, it is stored as an unsigned double word.?
The {$Z2} and {$Z4} states are useful for interfacing with C and C++ libraries, which usually represent enumerated types as words or double words.
Note: Note: For backwards compatibility with early versions of Delphi and CodeGear Pascal, the directives {$Z-} and {$Z+} are also supported. They correspond to {$Z1} and {$Z4}, respectively.
例子如下:
http://www.delphibasics.co.uk/RTL.asp?Name=$Z
Example code : Various enum type sizes
type
? {$Z1}
? TCars1? = (Rover, Jaguar, Honda);??? // Will fit into 1 byte
? TFruit1 = (Banana=255, Apple, Pear); // Will exceed one byte
? {$Z4}
? TCars2? = (Ford, Nissan, Vauxhall);? // Now uses 4 bytes
? TFruit2 = (Orange=255, Plum, Grape); // Now uses 4 bytes
begin
? ShowMessage('TCars1? size = '+IntToStr(SizeOf(TCars1)));
? ShowMessage('TFruit1 size = '+IntToStr(SizeOf(TFruit1)));
? ShowMessage('TCars2? size = '+IntToStr(SizeOf(TCars2)));
? ShowMessage('TFruit2 size = '+IntToStr(SizeOf(TFruit2)));
end;
?? TCars1? size = 1
?? TFruit1 size = 2
?? TCars2? size = 4
?? TFruit2 size = 4
轉(zhuǎn)載于:https://www.cnblogs.com/h2zZhou/p/5000153.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的DELPHI设置枚举类型size的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奇骏强上“三缸机”被教做人!日产向中国市
- 下一篇: 燃油车主请等等!国内油价将迎调整窗口 或