delphi 關于 array of const

//這是在 System 單元定義的一組標識數(shù)據(jù)類型的常量:
vtInteger = 0;
vtBoolean = 1;
vtChar = 2;
vtExtended = 3;
vtString = 4;
vtPointer = 5;
vtPChar = 6;
vtObject = 7;
vtClass = 8;
vtWideChar = 9;
vtPWideChar = 10;
vtAnsiString = 11;
vtCurrency = 12;
vtVariant = 13;
vtInterface = 14;
vtWideString = 15;
vtInt64 = 16;

//這是定義在 System 單元關于數(shù)據(jù)類型的一個結構:
TVarRec = record
case Byte of
vtInteger: (VInteger: Integer; VType: Byte);
vtBoolean: (VBoolean: Boolean);
vtChar: (VChar: Char);
vtExtended: (VExtended: PExtended);
vtString: (VString: PShortString);
vtPointer: (VPointer: Pointer);
vtPChar: (VPChar: PChar);
vtObject: (VObject: TObject);
vtClass: (VClass: TClass);
vtWideChar: (VWideChar: WideChar);
vtPWideChar: (VPWideChar: PWideChar);
vtAnsiString: (VAnsiString: Pointer);
vtCurrency: (VCurrency: PCurrency);
vtVariant: (VVariant: PVariant);
vtInterface: (VInterface: Pointer);
vtWideString: (VWideString: Pointer);
vtInt64: (VInt64: PInt64);
end;
--------------------------------------------------------------------------------
作為參數(shù)的開放數(shù)組, 有時數(shù)組的成員類型是不確定的, 此時應該使用 array of const 定義; 詳細舉例:
--------------------------------------------------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{把不同數(shù)據(jù)類型返回字符串的函數(shù)}
function Fun1(arr: array of const): string;
var
i: Integer;
begin
Result := '';
for i := Low(arr) to High(arr) do
begin
case arr[i].VType of
vtInteger : Result := Result + IntToStr(arr[i].VInteger) + ' ';
vtBoolean : Result := Result + BoolToStr(arr[i].VBoolean, True) + ' ';
vtChar : Result := Result + arr[i].VChar + ' ';
vtExtended : Result := Result + FloatToStr(arr[i].VExtended^) + ' ';
vtString : Result := Result + PShortString(arr[i].VString)^ + ' ';
vtPointer : Result := Result + IntToStr(Integer(arr[i].VPointer)) + ' ';
vtPChar : Result := Result + arr[i].VPChar + ' ';
vtObject : Result := Result + arr[i].VObject.ClassName + ' ';
vtClass : Result := Result + arr[i].VClass.ClassName + ' ';
vtWideChar : Result := Result + arr[i].VWideChar + ' ';
vtPWideChar : Result := Result + arr[i].VPWideChar + ' ';
vtAnsiString: Result := Result + PAnsiChar(arr[i].VAnsiString)^ + ' ';
vtCurrency : Result := Result + CurrToStr(arr[i].VCurrency^) + ' ';
vtVariant : Result := Result + string(arr[i].VVariant^) + ' ';
vtInterface : Result := Result + IntToStr(Integer(arr[i].VInterface)) + ' ';
vtWideString: Result := Result + PWideChar(arr[i].VWideString) + ' ';
vtInt64 : Result := Result + IntToStr(arr[i].VInt64^) + ' ';
end;
end;
end;

{簡化上一個函數(shù)}
function Fun2(const arr: array of const): string;
var
i: Integer;
const
n = #32;
begin
Result := '';
for i := Low(arr) to High(arr) do with arr[i] do
begin
case VType of
0 : Result := Result + IntToStr(VInteger) + n;
1 : Result := Result + BoolToStr(VBoolean, True) + n;
2 : Result := Result + VChar + n;
3 : Result := Result + FloatToStr(VExtended^) + n;
4 : Result := Result + PShortString(VString)^ + n;
5 : Result := Result + IntToStr(Integer(VPointer)) + n;
6 : Result := Result + VPChar + n;
7 : Result := Result + VObject.ClassName + n;
8 : Result := Result + VClass.ClassName + n;
9 : Result := Result + VWideChar + n;
10: Result := Result + VPWideChar + n;
11: Result := Result + PAnsiChar(VAnsiString)^ + n;
12: Result := Result + CurrToStr(VCurrency^) + n;
13: Result := Result + string(VVariant^) + n;
14: Result := Result + IntToStr(Integer(VInterface)) + n;
15: Result := Result + PWideChar(VWideString) + n;
16: Result := Result + IntToStr(VInt64^) + n;
end;
end;
end;

{獲取類型名的函數(shù)}
function Fun3(const arr: array of const): string;
var
i: Integer;
const
n = sLineBreak;
begin
Result := '';
for i := Low(arr) to High(arr) do with arr[i] do
begin
case VType of
0 : Result := Result + 'Integer' + n;
1 : Result := Result + 'Boolean' + n;
2 : Result := Result + 'Char' + n;
3 : Result := Result + 'Extended' + n;
4 : Result := Result + 'String' + n;
5 : Result := Result + 'Pointer' + n;
6 : Result := Result + 'PChar' + n;
7 : Result := Result + 'Object' + n;
8 : Result := Result + 'Class' + n;
9 : Result := Result + 'WideChar' + n;
10: Result := Result + 'PWideChar' + n;
11: Result := Result + 'AnsiString'+ n;
12: Result := Result + 'Currency' + n;
13: Result := Result + 'Variant' + n;
14: Result := Result + 'Interface' + n;
15: Result := Result + 'WideString'+ n;
16: Result := Result + 'Int64' + n;
end;
end;
end;

{測試}
procedure TForm1.Button1Click(Sender: TObject);
var
a: Integer;
b: Boolean;
c: Char;
d: Extended;
e: ShortString;
f: Pointer;
g: PChar;
h: TButton;
i: TClass;
j: WideChar;
k: PWideChar;
l: AnsiString;
m: Currency;
n: Variant;
o: IInterface;
p: WideString;
q: Int64;
begin
a := 1;
b := True;
c := 'a';
d := 2;
e := 'S';
f := Pointer(3);
g := 'P';
h := TButton(Sender);
i := TForm;
j := #19975;
k := '一';
l := 'A';
m := 4;
n := 5;
//o;
p := '萬一';
q := 7;

ShowMessage(Fun1([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q]));
ShowMessage(Fun2([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q]));
{結果如下:}
{1 True a 2 S 3 P TButton TForm 萬 一 A 4 5 0 萬一 7 }

ShowMessage(Fun3([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q]));
{結果如下:
Integer
Boolean
Char
Extended
String
Pointer
PChar
Object
Class
WideChar
PWideChar
AnsiString
Currency
Variant
Interface
WideString
Int64
}
end;

{我試試沒有在 TVarRec 中的類型是怎么歸類的}
procedure TForm1.Button2Click(Sender: TObject);
var
a: Byte;
b: Word;
c: Cardinal;
d: Double;
e: Real;
f: TStrings;
begin
ShowMessage(Fun3([a,b,c,d,e,f]));
{結果如下:
Integer
Integer
Integer
Extended
Extended
Object
}
end;

end.
--------------------------------------------------------------------------------
從這個例子中還能得到的啟示是(根據(jù)網友的提示, 下面可能理解錯了!):
--------------------------------------------------------------------------------

//不超過 4 字節(jié)的簡單類型, 在內存中只有一個存放處.
Integer;
Boolean;
Char;
WideChar;

//超過 4 字節(jié)的類型(包括字符串), 在內存中有兩個存放處: 一個是指針位置; 一個是數(shù)據(jù)位置.
Int64;
Extended;
Currency;
Variant;
ShortString;
AnsiString;
WideString;

//指針: 只有 4 字節(jié)大小.
Pointer;
PChar(PAnsiChar);
PWideChar;

//對象: 復雜了...
Object
Class;
IInterface;