相关疑难解决方法(0)

将对象转换为接口类型,不将TInterfacedObject作为基类

我需要一个没有引用计数的类实现接口.我做了以下事情:

  IMyInterface = interface(IInterface)
      ['{B84904DF-9E8A-46E0-98E4-498BF03C2819}'] 
      procedure InterfaceMethod;
  end;

  TMyClass = class(TObject, IMyInterface)
  protected
      function _AddRef: Integer;stdcall;
      function _Release: Integer;stdcall;
      function QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
  public
      procedure InterfaceMethod;
  end;

  procedure TMyClass.InterfaceMethod;
  begin
      ShowMessage('The Method');
  end;

  function TMyClass.QueryInterface(const IID: TGUID; out Obj): HResult;
  begin
      if GetInterface(IID, Obj) then
          Result := 0
      else
          Result := E_NOINTERFACE;
  end;

  function TMyClass._AddRef: Integer;
  begin
      Result := -1;
  end;

  function TMyClass._Release: Integer;
  begin
      Result := -1;
  end;
Run Code Online (Sandbox Code Playgroud)

缺少引用计数工作正常.但我担心的是,我不能投TMyClassIMyInterface使用as操作:

var …
Run Code Online (Sandbox Code Playgroud)

delphi interface delphi-xe2

9
推荐指数
1
解决办法
4371
查看次数

标签 统计

delphi ×1

delphi-xe2 ×1

interface ×1