函数指针应该也是一个指针把,所以我想用int型来保存其地址数据。
可是好像强制转换不行,我用内存拷贝也不行。
大虾们指点一下,目的就是用通过一个整数来传递函数指针,达到函数的调用和实现分离。
//------------------------------------------------
type
    PNotifyMsgs = procedure of object;var
    iTest: Integer;
    pFunc1,pFunc2 : PNotifyMsgs;
begin
pFunc1 := aProcedure;//将一个现有函数赋给它
CopyMemory(@iTest, @pFunc1, SizeOf(Integer));
CopyMemory(@pFunc2, @iTest, SizeOf(Integer));
pFunc2;
end;

解决方案 »

  1.   

    type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
        procedure Test;
      public
        { Public declarations }
      end;type
      PNotifyMsgs = procedure of object;
    var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    var
      iTest: Integer;
      pFunc1,pFunc2 : PNotifyMsgs;
    begin
      pFunc1 :=test;//将一个现有函数赋给它
      iTest:=Integer(@pFunc1);
      if Pointer(iTest)<>nil then begin
        pFunc1;
      end;
    end;procedure TForm1.Test;
    begin
      ShowMessage('OK');
    end;
      

  2.   

    参考:
    type
      TForm1 = class(TForm)
      ......
      published
        { Public declarations }    function CheckMP3(s:string):boolean;
      end;function TForm1.CheckMP3(s:String):boolean;
    begin
      if pos('.mp3',lowercase(s))<>0 then Result:=true else Result:=false;
    end;procedure TForm1.BtnClick(Sender: TObject);
    var CheckFunc: function(s:string):boolean of object; 
    begin 
      TMethod(CheckFunc).Code :=MethodAddress('CheckMP3');  
      TMethod(CheckFunc).Data := self ;
      if CheckFunc('1.mp3') then ShowMessage('ok'); 
     end;
      

  3.   

    to cncharles(旺仔) :
    怎么再把这个整数iTest赋给另一个函数指针pFunc2
    ??
      

  4.   

    to jinjintalk(西门吹雪)
    从这个例子看好像一个整数标识不了一个函数指针
    需要两个?
      

  5.   

    type
      TForm1 = class(TForm)
        btn1: TButton;
        procedure btn1Click(Sender: TObject);
      private
        { Private declarations }
        procedure Test;
      public
        { Public declarations }
      end;type
      PNotifyMsgs = procedure of object;
    var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    var
      iTest: Integer;
      pFunc1,pFunc2 : PNotifyMsgs;
    begin
      pFunc1 :=test;//将一个现有函数赋给它
      iTest:=Integer(@pFunc1);
      if Pointer(iTest)<>nil then begin
        @pFunc2:=Pointer(iTest); //就这样了, 会不会把你搞晕头
        pFunc2;
      end;
    end;procedure TForm1.Test;
    begin
      ShowMessage('OK');
    end;
      

  6.   

    @pFunc2:=Pointer(iTest); //就这样了, 会不会把你搞晕头
    这句真得很难理解~~~