delphi - 指向(子)方法函数的指针?

标签 delphi pointers delphi-7

如果您想使用方法的指针作为参数,则需要将该方法键入作为对象的函数,就像这样 :

type TAcceptor = function(filename:string):boolean of object;
function acceptor(filename:string):boolean; 
begin 
  result := filename <> ''; 
end;

如果你想使用子方法的指针怎么办?它不起作用:

procedure TForm1.Button1Click(Sender:TObject);
  function acceptor(filename:string):boolean of object;
  begin 
    result := filename <> ''; 
  end;
begin
end;

发生错误:;预期但发现了!

问题:有没有子函数指针?我可以转换吗?

最佳答案

我不明白这怎么可能。

http://docwiki.embarcadero.com/RADStudio/XE6/en/Procedural_Types

如果你看一下 method pointers部分,它特别指出不能使用嵌套过程和函数:

"Nested procedures and functions (routines declared within other routines) cannot be used as procedural values, nor can predefined procedures and functions."

您也许可以使用匿名方法来解决这个问题。像这样的东西:

procedure TForm1.Button1Click(Sender:TObject);
begin
  DoSomethingWithAcceptor(function(FileName: string): Boolean
  begin 
    Result := FileName <> '';
  end);
end;

关于delphi - 指向(子)方法函数的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977215/

相关文章:

c++ - 将非指针分配给指针? (在 C++ 中子类化指针类型?)

delphi - Windows Message lparam转换

Delphi unicode 移植 : Incompatible types: 'Char' and 'AnsiChar' error with Win32 functions like CharToOEM?

android - delphi xe6 firemonkey 更改表单样式运行时

delphi - 如何将位图转换为视频?

c - c 中的 rand() 出现问题

c - 二维字符数组中的 C 指针问题

delphi - C++ 到 Delphi : variable in FOR loop with assigned declaration

delphi - 使用 TLS 从 DELPHI(Indy) 使用 Gmails 传出 SMTP

delphi - 在另一个窗体的 OnDestroy 事件中释放窗体时发生访问冲突