Delphi XE 中的类型转换问题

发布于 2024-12-04 10:23:48 字数 521 浏览 0 评论 0原文

我尝试以这种方式执行程序列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-12-11 10:23:48

类型转换是无效的,因为您无法将方法指针安装到指针,方法指针实际上是两个指针,第一个是方法的地址,第二个是对该方法所属对象的引用。请参阅文档中的过程类型。这在任何版本的 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.

七分※倦醒 2024-12-11 10:23:48

Sertac 已经解释了为什么您的代码不起作用。为了在 Delphi 7 中实现这样的列表,你可以这样做。

type
  PProc = ^TProc;
  TProc = procedure of object;

  TProcList = class(TList)
  private
    FList: TList;
    function GetCount: Integer;
    function GetItem(Index: Integer): TProc;
    procedure SetItem(Index: Integer; const Item: TProc);
  public
    constructor Create;
    destructor Destroy; override;
    property Count: Integer read GetCount;
    property Items[Index: Integer]: TProc read GetItem write SetItem; default;
    function Add(const Item: TProc): Integer;
    procedure Delete(Index: Integer);
    procedure Clear;
  end;

type
  TProcListContainer = class(TList)
  protected
    procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  end;

procedure TProcListContainer.Notify(Ptr: Pointer; Action: TListNotification);
begin
  inherited;
  case Action of
  lnDeleted:
    Dispose(Ptr);
  end;
end;

constructor TProcList.Create;
begin
  inherited;
  FList := TProcListContainer.Create;
end;

destructor TProcList.Destroy;
begin
  FList.Free;
  inherited;
end;

function TProcList.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TProcList.GetItem(Index: Integer): TProc;
begin
  Result := PProc(FList[Index])^;
end;

procedure TProcList.SetItem(Index: Integer; const Item: TProc);
var
  P: PProc;
begin
  New(P);
  P^ := Item;
  FList[Index] := P;
end;

function TProcList.Add(const Item: TProc): Integer;
var
  P: PProc;
begin
  New(P);
  P^ := Item;
  Result := FList.Add(P);
end;

procedure TProcList.Delete(Index: Integer);
begin
  FList.Delete(Index);
end;

procedure TProcList.Clear;
begin
  FList.Clear;
end;

免责声明:完全未经测试的代码,使用时需自行承担风险。

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.

type
  PProc = ^TProc;
  TProc = procedure of object;

  TProcList = class(TList)
  private
    FList: TList;
    function GetCount: Integer;
    function GetItem(Index: Integer): TProc;
    procedure SetItem(Index: Integer; const Item: TProc);
  public
    constructor Create;
    destructor Destroy; override;
    property Count: Integer read GetCount;
    property Items[Index: Integer]: TProc read GetItem write SetItem; default;
    function Add(const Item: TProc): Integer;
    procedure Delete(Index: Integer);
    procedure Clear;
  end;

type
  TProcListContainer = class(TList)
  protected
    procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  end;

procedure TProcListContainer.Notify(Ptr: Pointer; Action: TListNotification);
begin
  inherited;
  case Action of
  lnDeleted:
    Dispose(Ptr);
  end;
end;

constructor TProcList.Create;
begin
  inherited;
  FList := TProcListContainer.Create;
end;

destructor TProcList.Destroy;
begin
  FList.Free;
  inherited;
end;

function TProcList.GetCount: Integer;
begin
  Result := FList.Count;
end;

function TProcList.GetItem(Index: Integer): TProc;
begin
  Result := PProc(FList[Index])^;
end;

procedure TProcList.SetItem(Index: Integer; const Item: TProc);
var
  P: PProc;
begin
  New(P);
  P^ := Item;
  FList[Index] := P;
end;

function TProcList.Add(const Item: TProc): Integer;
var
  P: PProc;
begin
  New(P);
  P^ := Item;
  Result := FList.Add(P);
end;

procedure TProcList.Delete(Index: Integer);
begin
  FList.Delete(Index);
end;

procedure TProcList.Clear;
begin
  FList.Clear;
end;

Disclaimer: completely untested code, use at your own risk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文