Delphi XE 中的类型转换问题
我尝试以这种方式执行程序列表:
type
TProc = procedure of object;
TMyClass=class
private
fList:Tlist;
function getItem(index:integer):TProc;
{....}
public
{....}
end;
implementation
{....}
function TMyClass.getItem(index: Integer): TProc;
begin
Result:= TProc(flist[index]);// <--- error is here!
end;
{....}
end.
并收到错误:
E2089 类型转换无效
如何修复? 正如我所看到的,我可以创建一个只有一个属性 Proc:TProc;
的假类并列出它。但我觉得这是一个不好的方法,不是吗?
PS:项目必须兼容delphi-7。
I try to do list of procedures this way:
type
TProc = procedure of object;
TMyClass=class
private
fList:Tlist;
function getItem(index:integer):TProc;
{....}
public
{....}
end;
implementation
{....}
function TMyClass.getItem(index: Integer): TProc;
begin
Result:= TProc(flist[index]);// <--- error is here!
end;
{....}
end.
and get error:
E2089 Invalid typecast
How can I fix it?
As I see, I can make a fake class with only one property Proc:TProc;
and make list of it. But I feel that it's a bad way, isn't it?
PS: project have to be delphi-7-compatible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类型转换是无效的,因为您无法将方法指针安装到指针,方法指针实际上是两个指针,第一个是方法的地址,第二个是对该方法所属对象的引用。请参阅文档中的过程类型。这在任何版本的 Delphi 中都不起作用。
The typecast is invalid because you can not fit a method pointer to a pointer, a method pointer is in fact two pointers first being the address of the method and the second being a reference to the object that the method belongs. See Procedural Types in the documentation. This will not work in any version of Delphi.
Sertac 已经解释了为什么您的代码不起作用。为了在 Delphi 7 中实现这样的列表,你可以这样做。
免责声明:完全未经测试的代码,使用时需自行承担风险。
Sertac has explained why your code doesn't work. In order to implement a list of such things in Delphi 7 you can do something like this.
Disclaimer: completely untested code, use at your own risk.