gpt4 book ai didi

delphi - delphi获取成员函数指针

转载 作者:行者123 更新时间:2023-12-03 15:46:45 26 4
gpt4 key购买 nike

是否有一些技巧如何在 Lazarus/delphi 中获取成员函数的指针?我有这段代码无法编译...

错误是在德尔福:
variable required

在拉撒路:
Error: Incompatible types: got "<procedure variable type of function(Byte):LongInt of object;StdCall>" expected "Pointer"


代码:

  TClassA = class
public
function ImportantFunc(AParameter: byte): integer; stdcall;
end;

TClassB = class
public
ObjectA: TClassA;
ImportantPtr: pointer;
procedure WorkerFunc;
end;

function TClassA.ImportantFunc(AParameter: byte): integer; stdcall;
begin
// some important stuff
end;

procedure TClassB.WorkerFunc;
begin
ImportantPtr := @ObjectA.ImportantFunc; // <-- ERROR HERE
end;

谢谢!

最佳答案

成员函数不能用单个指针表示。它需要两个指针,一个用于实例,一个用于代码。但这是实现细节,您只需要使用方法类型:

type
TImportantFunc = function(AParameter: byte): integer of object; stdcall;

然后您可以将 ImportFunc 分配给该类型的变量。

由于您正在使用 stdcall,我怀疑您正在尝试将其用作 Windows 回调。这对于成员函数来说是不可能的。您需要一个具有全局作用域的函数,或者一个静态函数。

关于delphi - delphi获取成员函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com