有没有一些技巧如何在 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;
Thanks!
成员函数不能用单个指针表示。它需要两个指针,一个用于实例,一个用于代码。但这是实现细节,您只需要使用方法类型:
type
TImportantFunc = function(AParameter: byte): integer of object; stdcall;
然后您可以将ImportantFunc 分配给该类型的变量。
由于您正在使用 stdcall,我怀疑您正在尝试将其用作 Windows 回调。这对于成员函数来说是不可能的。您需要一个具有全局范围的函数或静态函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)