是啊, case 在编程中没有任何作用.
应该是编译器识别 pascal union 的标志.
unnion 在 Delphi 中的使用,主要用于兼容不同的类型:
(因为可以使用类型转换TWordRec(), 所以节省空间的作用, 看不出来啊)
var
Bs: array[0..1] of byte;
begin
Bs[0] := $AA;
Bs[1] := $01;
//W := $01AA;
W2 := TWordRec(Bs);
H := W2.Hi;
L := W2.Lo;
ShowMessage('1. ' + IntToHex(Word(Bs), 4) + '-' + IntToHex(H, 2) + '-' + IntToHex(L, 2));
end;
=================================================
Delphi SysUtils.pas 中的 Union:
{ Type conversion records }
WordRec = packed record
case Integer of
0: (Lo, Hi: Byte);
1: (Bytes: array [0..1] of Byte);
end;
LongRec = packed record
case Integer of
0: (Lo, Hi: Word);
1: (Words: array [0..1] of Word);
2: (Bytes: array [0..3] of Byte);
end;
Int64Rec = packed record
case Integer of
0: (Lo, Hi: Cardinal);
1: (Cardinals: array [0..1] of Cardinal);
2: (Words: array [0..3] of Word);
3: (Bytes: array [0..7] of Byte);
end;
function TThreadLocalCounter2.HashIndex: Byte;
var
H: Word;
begin
H := Word(GetCurrentThreadID);
Result := (WordRec(H).Lo xor WordRec(H).Hi) and 15;
end;
..............
|