我在Vcl源码中发现record类型中的成员变量可以声明为一个函数,可是那个函数又是如何实现的呢。
比如下面这个例子。type
  aa=record
    tmp:function(value:integer):integer;
end;下面是函数的实现
function tmd(value:integer):integer;
begin
  result:=value+1;
end;这样编译是可以通过的。
但是调用就不行了。一调用时就有异常出现。不知是为什么,请教各位了。

解决方案 »

  1.   

    请参考我的文章:
    http://blog.csdn.net/maozefa/archive/2007/08/27/1760612.aspx
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;function AddInt(var aint:integer):integer;
    type
      TForm1 = class(TForm)
        CheckBox1: TCheckBox;
        CheckBox2: TCheckBox;
        CheckBox3: TCheckBox;
        CheckBox4: TCheckBox;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    pFunctionManager=^TFunctionManager;
    TFunctionManager=record
       AddMethod:function (var aint:integer):integer;//一个函数指针罢了,应用时,指向一个同类型的函数;
    end;var
      Form1: TForm1;implementation function AddInt(var aint:integer):integer;//定义一个与记录类型域(AddMethod)相同的函数类型并实现;
      begin
        aint:=aint+55;
        result:=aint;
      end;
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      p:pFunctionManager;
      i:integer;
      p2:tFunctionManager;
    begin
      new(p);
      try
         p^.AddMethod:=@AddInt;
         i:=100;
         p^.AddMethod(i);
         showmessage(inttostr(i));
      finally
         dispose(p);
      end;  p2.AddMethod:=@addint;
      i:=5;
      p2.AddMethod(i);
      showmessage(inttostr(i));
    end;//以上代码,在Delphi7+winxp下调试通过.
      

  3.   

    借楼主贴子,测试下代码着色在回贴中的效果。哈哈~~
    procedure TForm1.Button1Click(Sender: TObject); 
    var 
      p:pFunctionManager; 
      i:integer; 
      p2:tFunctionManager; 
    begin 
      new(p); 
      try 
         p^.AddMethod:=@AddInt; 
         i:=100; 
         p^.AddMethod(i); 
         showmessage(inttostr(i)); 
      finally 
         dispose(p); 
      end;   p2.AddMethod:=@addint; 
      i:=5; 
      p2.AddMethod(i); 
      showmessage(inttostr(i)); 
    end;