有关函数指针?
function test: string;
begin
result := function pointer test;
end;
procedure tform1.button1click(sender: tobject);
var
func: function: string;
begin
func := test;
caption := func;
end;
//上面的调用一切正常
//现在我将函数写入类中调用
tbase = class
public
function test: string;
end;
function tbase.test: string;
begin
result := object function pointer test;
end;
procedure tform1.button2click(sender: tobject);
var
base: tbase;
func: function: string of object;
begin
base := tbase.create;
try
//这样调用tmethod(func).code也是nil
// tmethod(func).code := base.methodaddress(test);
tmethod(func).code := tbase.methodaddress(test);
if assigned(tmethod(func).code) then
caption := func;
finally
base.free;
end;
end;
为什么tmethod(func).code还是nil,如果调用类中的函数指针应该怎样定义和调用?
推荐阅读
一个方法是这样 tmethod(func).code := @tbase.test;
另一个方法是
tbase = class
published
function test: string; //声明为公布的
end;
把tbase 改成这样
tbase = class
published
function test: string;
end;
returns the address of a published method.
=========================================
class function methodaddress(const name: shortstring): pointer;
description
methodaddress is used internally by the vcl streaming system. when reading an event property from a stream, methodaddress converts a method name, specified by name, to a pointer containing the method address. there should be no need to call methodaddress directly.
if name does not specify a published method for the object, methodaddress returns nil.


讨论区