代码之家  ›  专栏  ›  技术社区  ›  Kenneth Cochran

在Delphi中将接口引入现有的类层次结构

  •  4
  • Kenneth Cochran  · 技术社区  · 14 年前

    3 回复  |  直到 14 年前
        1
  •  4
  •   Ondrej Kelle    14 年前

    如果已经有使用该类的现有代码,则可能需要对其进行大量修改,以保留对接口的引用,而不是对对象实例的引用。接口是自动计数和释放的引用,因此,对实现者实例的任何引用都将成为无效指针。

        2
  •  3
  •   Brian Frost    14 年前

    type
    
    
      TYourAncestor = class( TInterfacedObject )
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
    
      end;
    
    
    
    implementation
    
    
    
    function TYourAncestor.QueryInterface(const IID: TGUID; out Obj): HResult;
    const
      E_NOINTERFACE = HResult($80004002);
    begin
      if GetInterface(IID, Obj) then Result := 0 else Result := E_NOINTERFACE;
    end;
    
    
    function TYourAncestor._AddRef: Integer;
    begin
      Result := -1   // -1 indicates no reference counting is taking place
    end;
    
    function TYourAncestor._Release: Integer;
    begin
      Result := -1   // -1 indicates no reference counting is taking place
    end;
    
        3
  •  2
  •   Mason Wheeler    14 年前

    除了实例大小中的几个额外字节之外,不。这可能是最好的方法。