Delphi中函数指针的使用

本文介绍Delphi中函数指针的基本定义与使用方法,包括如何声明函数指针类型、定义函数并将其作为参数传递,以及如何使用这些函数指针进行动态函数调用。此外还提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

delphi中的函数指针如何定义和使用
var
  p:Pointer;
begin
  p:=@函数名称
end;

调用时
p(参数表);

就行了

{-----------------------------Delphi中函数指针的使用-----------------------------}

 

Delphi中可以通过函数指针把一个函数作为参数来传递,然后在另外一个函数中调用。

1) 首先,申明函数指针类型TFunctionParameter
    type
          TFunctionParameter = function(const value : integer) : string; //函数指针

2) 定义准备被作为参数传递的函数
    function One(const value : integer) : string;                            //函数-实例1
    begin
      result := IntToStr(value) ;
    end;

    function Two(const value : integer) : string;                            //函数-实例2
    begin
      result := IntToStr(2 * value) ;
    end;
     
3) 定义将要使用动态函数指针参数的函数
    function DynamicFunction(f : TFunctionParameter; const value : integer) : string;
    begin
      result := f(value) ;
    end;

4) 上面这个动态函数的使用实例
    var
      s : string;
    begin
      s := DynamicFunction(One,2006) ;
      ShowMessage(s) ; //will display "2006"

      s := DynamicFunction(Two,2006) ;
      ShowMessage(s) ; // will display "4012"
    end;


<====================================================================================================>
指向非对象(一般的)函数/过程的函数指针

 Pascal 中的过程类型与C语言中的函数指针相似,为了统一说法,以下称函数指针。函数指针的声明只需要参数列表;如果是函数,再加个返回值。例如声明一个过程类型,该类型带一个通过引用传递的整型参数:

type
  IntProc = procedure (var Num: Integer);

这个过程类型与任何参数完全相同的例程兼容,即用它声明的变量,可以指向任何此类函数,并通过其进行函数的调用。下面是一个兼容例程:

procedure DoubleTheValue (var Value: Integer);
begin
Value := Value * 2;
end;

函数指针能用于两种不同的目的:声明函数指针类型的变量;或者把函数指针作为参数传递给另一例程。利用上面给定的类型和过程声明,你可以写出下面的代码:

var
  IP: IntProc;
  X: Integer;
begin
  IP := DoubleTheValue;
  X := 5;
  IP (X);
end;

虽然这种调用方法比直接调用麻烦了,那么我们为什么要用这种方式呢?

(1)因为在某些情况下,调用什么样的函数需要在实际中(运行时)决定,你可以根据条件来判断,实现用同一个表达,调用不同的函数,很是灵活.

(2)利用函数指针我们可以实现委托,委托在.NEt中被发挥的淋漓尽致,但Delphi同样能实现

(3)实现回调机制

函数指针很有用啊,是高级程序员的必修。

例子

   1. {********************************************************
   2.   函数指针(指向一般函数和过程)
   3. UnitOwner:coco.zhang                          
   4.   Last Modified:2008-10-5  
   5. *********************************************************}
   6. unit DelegateUnit;
   7. interface
   8.      procedure Func1;
   9.      {定义两个函数型构相同但功能不同的函数}
  10.      function    FuncAdd(VarA , VarB : Integer):Integer;
  11.      function    FuncSub(VarA , VarB : Integer):Integer;
  12. type
  13.      DelegateFunc1 = procedure;
  14.      DelegateFuncCalc = function(VarA , VarB : Integer):Integer;
  15. var
  16.    I : Integer;
  17. implementation
  18.
  19.    procedure Func1;
  20.    begin
  21.        Writeln('Func1 was called!');
  22.    end;
  23.    function    FuncAdd(VarA , VarB : Integer):Integer;
  24.    begin
  25.         Result := VarA + VarB;
  26.    end;
  27.     function    FuncSub(VarA , VarB : Integer):Integer;
  28.    begin
  29.         Result := VarA - VarB;
  30.    end;
  31.    end.

客户端调用

   1. program Delegate;
   2. {$APPTYPE CONSOLE}
   3. uses
   4.   DelegateUnit;
   5. var
   6.    ADelegateFunc1 : DelegateFunc1;
   7.    ADelegateFuncCalc : DelegateFuncCalc;
   8. begin
   9.     {通过函数指针调用过程}
  10.     ADelegateFunc1  := Func1;
  11.     ADelegateFunc1 ;
  12.     {通过同种方式调用不同函数}
  13.     ADelegateFuncCalc  := FuncAdd;
  14.     Writeln(ADelegateFuncCalc(3,5));
  15.     ADelegateFuncCalc  := FuncSub;
  16.     Writeln(ADelegateFuncCalc(3,5));
  17. end.

运行结果
D:/Projects/Delphi7/src/GofProjects/Delegate>Delegate
Func1 was called!
8
-2

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值