type
  IIF = interface(IUnknown)
['{E351FAF7-1A12-4164-8866-7ED2D8599174}']
    procedure mm;
  end;  TIF = class(TObject, IIF)
  protected
    FRefCount: Integer;
    function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    procedure mm;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    class function NewInstance: TObject; override;
    property RefCount: Integer read FRefCount;
  end;
...
...
procedure TForm1.Button1Click(Sender: TObject);
var
  ti: IIF;
  t: tif;
begin
  t := tif.Create;
  ti := t as IIF;  //为什么不行  [Error] Unit1.pas(51): Operator not applicable to this ..

解决方案 »

  1.   

    年纪还小,无法回答,just up
      

  2.   

    TIF = class(TObject, IIF)   ->
    TIF = class(TObject, IIF, IInterface)When you use the as operator for dynamic binding on an interface, you should be aware of the following requirements:
    1)Explicitly declaring IInterface: Although all interfaces derive from IInterface, it is not sufficient, if you want to use the as operator, for a class to simply implement the methods of IInterface. This is true even if it also implements the interfaces it explicitly declares. The class must explicitly declare IInterface in its interface list.
    2)Using an IID: Interfaces can use an identifier that is based on a GUID (globally unique identifier). GUIDs that are used to identify interfaces are referred to as interface identifiers (IIDs). If you are using the as operator with an interface, it must have an associated IID. To create a new GUID in your source code you can use the Ctrl+Shift+G editor shortcut key.
      

  3.   

    type
      IIF = interface(IUnknown)
    ['{E351FAF7-1A12-4164-8866-7ED2D8599174}']
        procedure mm;
      end;  TIF = class(TinterfacedObject, IIF)
      protected
        FRefCount: Integer;
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
        procedure mm;
      public
        procedure AfterConstruction; override;
        procedure BeforeDestruction; override;
        class function NewInstance: TObject; override;
        property RefCount: Integer read FRefCount;
      end;
      

  4.   

    to  hahafan(注意!此人前途迷茫)
    [Error] Unit2.pas(13): Undeclared identifier: 'IInterface'
     因为我用delphi5 所以没有'IInterface'??to dinglinger(叮当) 
      I just want to know why my 不行..
      

  5.   

    to  hahafan(注意!此人前途迷茫)
      TIF = class(TObject, IIF)   ->
      TIF = class(TObject, IIF, IUnknown)
    你写错了吧 这样倒是可以 研究英文中
      

  6.   

    D7中
    IUnknown = IInterface