delphi中函数指针的用法
2007-05-10 17:29
330 查看
delphi中可以通过函数指针把一个函数作为参数来传递,然后在另外一个函数中调用。
1) 首先,申明函数指针类型TFunctionParameter。
type
TFunctionParameter = function(const value : integer) : string;
2) 定义准备被作为参数传递的函数
function One(const value : integer) : string;
begin
result := IntToStr(value) ;
end;
function Two(const value : integer) : string;
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;
1) 首先,申明函数指针类型TFunctionParameter。
type
TFunctionParameter = function(const value : integer) : string;
2) 定义准备被作为参数传递的函数
function One(const value : integer) : string;
begin
result := IntToStr(value) ;
end;
function Two(const value : integer) : string;
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;
相关文章推荐
- c/c++ 函数指针的用法
- c/c++ 函数指针的用法
- 函数指针的用法
- c 语言函数指针用法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)
- Delphi 中PChar()函数的用法
- C语言结构体指针,函数指针的用法
- Delphi 的内存操作函数(1): 给字符指针分配内存
- typedef函数指针用法
- delphi中指针的用法
- 类的成员函数指针和mem_fun适配器的用法
- Delphi指针相关函数
- (转)关于C++中函数指针的使用(包含对typedef用法的讨论)
- typedef 函数指针的用法
- c++ 函数返回指针 及用法
- const限定修饰符用法总结(常量,指针,迭代器,函数参数,成员函数)
- Delphi:记录类型、过程指针、函数(方法)指针字段的应用
- Delphi 函数指针 过程/函数类型
- 二级指针作为函数参数的典型用法
- 关于C++中函数指针的使用(包含对typedef用法的讨论)