谁能解释一下为什么下面的代码失败了?
type TIDEThemeObserverFunc = reference to procedure(foo: integer);
var fObserverFuncs: TList<TIDEThemeObserverFunc>
function RegisterEventObserver(aObserverFunc: TIDEThemeObserverFunc): Pointer;
begin
fObserverFuncs.Add(aObserverFunc);
Result := @aObserverFunc;
// line below somehow fails
assert(fObserverFuncs.IndexOf(TIDEThemeObserverFunc(Result)) <> -1);
end;
我假设匿名方法可以简单地通过指针进行转换和使用,但这似乎是一个错误的假设。此外,任何解释匿名方法在幕后如何实现的资源都会很棒。 TIA。
最佳答案
您应该使用 PPointer(@aObserverFunc)^ 而不是 @aObserverFunc 以避免断言失败。
@gabr:感谢您对我的博客的引用,但我建议首先阅读 the Stackoverflow user Barry Kelly博客作为更有效的信息来源。
关于delphi - 匿名方法转换为指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2309925/