之前应该参考一下: 关于开放数组参数

//这是在 System 单元定义的一组标识数据类型的常量:
vtInteger    = ;
vtBoolean    = ;
vtChar      = ;
vtExtended  = ;
vtString    = ;
vtPointer    = ;
vtPChar      = ;
vtObject    = ;
vtClass      = ;
vtWideChar  = ;
vtPWideChar  = ;
vtAnsiString = ;
vtCurrency  = ;
vtVariant    = ;
vtInterface  = ;
vtWideString = ;
vtInt64      = ; //这是定义在 System 单元关于数据类型的一个结构:
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;

作为参数的开放数组, 有时数组的成员类型是不确定的, 此时应该使用 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} {把不同数据类型返回字符串的函数}
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; {简化上一个函数}
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
      : Result := Result + IntToStr(VInteger)            + n;
      : Result := Result + BoolToStr(VBoolean, True)    + n;
      : Result := Result + VChar                        + n;
      : Result := Result + FloatToStr(VExtended^)        + n;
      : Result := Result + PShortString(VString)^        + n;
      : Result := Result + IntToStr(Integer(VPointer))  + n;
      : Result := Result + VPChar                        + n;
      : Result := Result + VObject.ClassName            + n;
      : Result := Result + VClass.ClassName              + n;
      : Result := Result + VWideChar                    + n;
      : Result := Result + VPWideChar                    + n;
      : Result := Result + PAnsiChar(VAnsiString)^      + n;
      : Result := Result + CurrToStr(VCurrency^)        + n;
      : Result := Result + string(VVariant^)            + n;
      : Result := Result + IntToStr(Integer(VInterface)) + n;
      : Result := Result + PWideChar(VWideString)        + n;
      : Result := Result + IntToStr(VInt64^)            + n;
    end;
  end;
end; {获取类型名的函数}
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
      : Result := Result + 'Integer'  + n;
      : Result := Result + 'Boolean'  + n;
      : Result := Result + 'Char'      + n;
      : Result := Result + 'Extended'  + n;
      : Result := Result + 'String'    + n;
      : Result := Result + 'Pointer'  + n;
      : Result := Result + 'PChar'    + n;
      : Result := Result + 'Object'    + n;
      : Result := Result + 'Class'    + n;
      : Result := Result + 'WideChar'  + n;
      : Result := Result + 'PWideChar' + n;
      : Result := Result + 'AnsiString'+ n;
      : Result := Result + 'Currency'  + n;
      : Result := Result + 'Variant'  + n;
      : Result := Result + 'Interface' + n;
      : Result := Result + 'WideString'+ n;
      : 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 := ;
  b := True;
  c := 'a';
  d := ;
  e := 'S';
  f := Pointer();
  g := 'P';
  h := TButton(Sender);
  i := TForm;
  j := #19975;
  k := '一';
  l := 'A';
  m := ;
  n := ;
  //o;
  p := '万一';
  q := ;   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.

从这个例子中还能得到的启示是(根据网友的提示, 下面可能理解错了!):

//不超过 4 字节的简单类型, 在内存中只有一个存放处.
Integer;
Boolean;
Char;
WideChar; //超过 4 字节的类型(包括字符串), 在内存中有两个存放处: 一个是指针位置; 一个是数据位置.
Int64;
Extended;
Currency;
Variant;
ShortString;
AnsiString;
WideString; //指针: 只有 4 字节大小.
Pointer;
PChar(PAnsiChar);
PWideChar; //对象: 复杂了...
Object
Class;
IInterface;

关于 array of const的更多相关文章

  1. Delphi中array of const应用

    Delphi的Format函数大家都用得很多,第二个参数用着确实很方便.最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Po ...

  2. C lang:Protect array data——Const

    Xx_Introduction Use pointer translate parameter array original data will change data,and use const p ...

  3. c++实现的Array数据结构

    1.Array.h,Array<T>的定义 template <class T> class Array { protected: T *data; //一个指向数组数据的指针 ...

  4. 从Chrome源码看JS Array的实现

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } .crayon-line spa ...

  5. ES6,Array.of()函数的用法

    ES6为Array增加了of函数用已一种明确的含义将一个或多个值转换成数组. 因为,用new Array()构造数组的时候,是有二意性的. 构造时,传一个参数,表示生成多大的数组. 构造时,传多个参数 ...

  6. C++中const指针用法汇总

    这里以int类型为例,进行说明,在C++中const是类型修饰符: int a; 定义一个普通的int类型变量a,可对此变量的值进行修改. const int a = 3;与 int const a ...

  7. [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)

    Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...

  8. [Functional Programming] Draw Items from One JavaScript Array to Another using a Pair ADT

    We want to be able to pick nine random cards from an array of twelve cards, but can run into problem ...

  9. [Tips + Javascript] Make a unique array

    To make an array uniqued, we can use Set() from Javascript. const ary = ["a", "b" ...

随机推荐

  1. 使用python库relate搭建LMS学习管理系统

    Relate is an Environment for Learning And TEaching Relate是在 Django上面构建的,可以快速搭建LMS系统,该系统可以方便学习管理和在线课程 ...

  2. 将Centos7的yum配置为阿里云的镜像(完美解决yum下载太慢的问题)

    2017-02-17 16:02:30 张老湿 阅读数 13768     http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0. ...

  3. django后台标题替换

    在制作django后台的时候,默认的django admin界面标题为“django管理”,找了许多的资料都很麻烦,偶与好友一起探讨,找到了新的解决方法 在django的py文件中插入 from dj ...

  4. Win10 C盘 系统和保留 占用空间 非常大

    Win10 C盘 系统和保留 占用空间 非常大今天在写代码的时候,突然发现Redis起不来了,一看原因,是因为C盘空间不足.然后,我看了下C盘,发现...一个叫系统和保留的东西,居然占了110G的空间 ...

  5. Android笔记(五十六) Android四大组件之一——ContentProvider,实现自己的ContentProvider

    有时候我们自己的程序也需要向外接提供数据,那么就需要我们自己实现ContentProvider. 自己实现ContentProvider的话需要新建一个类去继承ContentProvider,然后重写 ...

  6. 【CMDB】获取服务器数据

    一.通过agent的方式 原理:服务器定制执行py文件通过subprocess模块采集数据发送给数据收集的机器 数据收集的机器:192.168.11.62 服务器:192.168.11.169 数据收 ...

  7. pip 和pip3的区别

    前言装完python3后发现库里面既有pip也有pip3,不知道它们的区别,因此特意去了解了一下. 解释先搜索了一下看到了如下的解释, 安装了python3之后,库里面既会有pip3也会有pip 1. ...

  8. Wireshark 分析Linux SSh 远程登录延迟问题

    1.PuTTy远程登录延迟的分析 现象问题描述:在使用kali linux 的时候喜欢在后台运行而在Windows主机系统上安装PuTTY来实现远程登录 发现每次输入密码的时候会存在延迟10s的情况, ...

  9. flutter入门开发的一些坑

    flutter入门开发的一些坑 很久没写博客了,最近在准备物联网比赛,顺便抽出时间学习了一下flutter,花了近2周完成了一个查看博客博文的一个小的APPdemo,随便截了两张图,如下: 首页 博客 ...

  10. APC (Asynchronous Procedure Call)

    系统创建新线程时,会同时创建与这个线程相关联的队列,即异步过程调用(APC)的队列. 一些异步操作可以通过加入APC来实现,比如我现在学习的IO请求/完成. BOOL ReadFileEx( HAND ...