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,如果调用类中的函数指针应该怎样定义和调用?
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,如果调用类中的函数指针应该怎样定义和调用?
解决方案 »
- RP爆发了,参加“虚拟社区研究调查”得了30元充值卡,散分了!
- 条码扫描(激活窗体)
- 读取和设置文件属性中Custom页
- 求教高手关于DataSet的问题?
- 在线等待,急,delphi+access sql语句
- 关于Report Machine问题
- 请问cannot open clipboard错误是怎么回事?
- 我用WINEXEC(CMDSTR,SW_SHOWNORMAL)语句对oracle数据库进行备份,但是不知道该如何取的数据库在DOS下的相关导出信息。请各位高手帮忙!!
- To zswang(伴水)(被黑中)!!!!!!!!!!!
- 学Delphi的同志们,你们都有转向学Java么,用Java么?
- 关于B/S系统的设计,请大家来讨论讨论。
- 为何我使用ADOConnection,ADOTable,DataSource导入EXCEL数据到DBGRID时,
另一个方法是
TBase = class
published
function Test: string; //声明为公布的
end;
把分都给他吧!————————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
————————————————————————————————————
TBase = class
published
function Test: string;
end;Returns the address of a published method.
=========================================class function MethodAddress(const Name: ShortString): Pointer;DescriptionMethodAddress 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.
改为
TMethod(Func).Code := @TBase.Test;
注意不管是对象方法还是类方法,它的地址都在存储在类中,
所以此时不能使用@Base.Test;