Generic method returning generic interface in Delphi 2010

前端 未结 2 1373
南旧
南旧 2021-01-02 17:58

Given the code below, wich is a very trimmed down version of the actual code, I get the following error:

[DCC Error] Unit3.pas(31): E2010 Incompatible types: \'IXL

2条回答
  •  天涯浪人
    2021-01-02 18:36

    With three minor modification (IInterface, FindAll with "S: class" [Thanks Mason] and the typecasts in FindAll) I got it compiling.

    Full code:

    unit Unit16;
    
    interface
    
    uses
      Generics.Collections;
    
    type
      IXList = interface
      end;
    
      TXList = class(TList, IInterface, IXList)
      protected
        FRefCount: Integer;
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
      public
        function Find: IXList;
        function FindAll: IXList;
      end;
    
    implementation
    uses Windows;
    
    function TXList.Find: IXList;
    begin
      Result := TXList.Create;
    end;
    
    function TXList.FindAll: IXList;
    begin
      Result := IXList(IUnknown(TXList.Create));
    end;
    
    function TXList.QueryInterface(const IID: TGUID; out Obj): HResult;
    begin
      Result := E_NoInterface;
    end;
    
    function TXList._AddRef: Integer;
    begin
      InterlockedIncrement(FRefCount);
    end;
    
    function TXList._Release: Integer;
    begin
      InterlockedDecrement(FRefCount);
      if FRefCount = 0 then Self.Destroy;
    end;
    
    end.
    

提交回复
热议问题