Delphi Union 使用
Union部分只能放在最后,这和C的不太一样,要放在前面先用Type定义一个单独的Union类型就可以了。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; type TMyUnion = packed record ID: Integer; // case Boolean of // true: (cmd: DWORD); // false: (hiCmd:Word;loCmd:Word); // case Byte of // 0: (cmd: DWORD); // 1: (hiCmd:Word;loCmd:Word); // case Integer of // 0: (cmd: DWORD); // 1: (hiCmd:Word;loCmd:Word); case aCmd:Integer of 0: (cmd: DWORD); 1: (hiCmd:Word;loCmd:Word); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var tmp: TMyUnion; begin tmp.ID := 10; tmp.cmd := $0000FFFF; tmp.aCmd := 100; ShowMessageFmt('%d',[SizeOf(TMyUnion)]); ShowMessageFmt('%d',[tmp.hiCmd]); ShowMessageFmt('%d',[tmp.aCmd]); Application.Terminate; end; end.