回调函数的问题?
源码如下:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils,Dialogs;
type
tintpoint=^integer;
type
tcallback=procedure (i:tintpoint);
procedure callback(i:tintpoint);
begin
if i^=0 then
begin
showmessage('well');
end
else
begin
showmessage('bad') ;
end;
end;
procedure mycall(call:tcallback);
var
int:integer;
begin
callback(@int);
end;
var
in1:integer;
begin
readln(in1);
mycall(@in1);
end.
也不知道这样是不是定义回调函数,还有就是程序老实输出bad,无法输出well,那里有错误请执教
[解决办法]
楼主没理解清楚回调函数的概念,举个例子来说明一下:
A函数提供了某种服务,不过这个服务不是实时的,在调用A函数之后需要过一会儿(这个时间长短不确定)才会结束。
现在B想调用A函数,并且想知道服务何时完成,于是B在调用A函数时同时告诉A函数另外一个函数C,A函数会在服务完成时去调用C函数。
这个C函数就是所谓的回调函数。
type tcallback = procedure (i: Integer); procedure callback(i: Integer);begin if i = 0 then showmessage('well') else showmessage('bad');end; procedure mycall(call: tcallback);begin call (Random(2)); //A回调Cend; begin mycall(callback); //B调用A,并且告诉A回调函数C是哪一下end.
[解决办法]
我也不太明白。。。