【问题标题】:Free Pascal multiple interfaces problemFree Pascal 多接口问题
【发布时间】:2011-08-27 23:56:38
【问题描述】:

这可能很困难——我在一个对象上使用了多个 CORBA 接口,所以它看起来像这样:

TBaseObject = class(TSuperBaseObject, IInterfaceA)
  function Afunction; // implemented from IInterfaceA
end;

TOtherObject = class(TBaseObject, IInterfaceB);  
  function Bfunction; // implemented from IInterfaceB
end;

现在我有一个接受变体的函数,如果该变体是一个对象,它假定该对象是一个 IInterfaceA 对象:

case var.vtype of
  ...
  vtObject     : begin
    Something := (var.vObject as IInterfaceA).AFunction; (1)
  end;
end;

现在,一旦我运行该代码并将 TOtherObject 传递给函数,在第 (1) 行中,BFunction 会被强制参数调用!

我做错了什么还是编译器中的错误?另外,有什么明智的方法可以在不改变类结构的情况下绕过它?

如果有人想尝试,请提供 EAccessViolation 的完整代码 - http://pastebin.com/D7sDpDHx

【问题讨论】:

    标签: interface casting corba freepascal


    【解决方案1】:

    将此作为错误报告给 FPC 错误跟踪器 - http://bugs.freepascal.org/view.php?id=20076

    事实证明,FPC 在内部无法识别 CORBA 接口。要解决问题,需要自己识别它们:

    type IInterfaceA = interface['interface_a']
       function AFunction;
    end;
    

    然后as 关键字将起作用。

    【讨论】:

      【解决方案2】:

      不确定FreePascal,但在Delphi中你会使用supports函数来查询接口。

      var
          IntfA : IInterfaceA;
          IntfB : IInterfaceB;
       begin 
       case var.vtype of
        ...
        vtObject : begin
                   if supports(var.vObject,IInterfaceA,IntfA) then
                     Something := IntfA.AFunction
                   else if supports(var.vObject,IInterfaceB,IntfB) then
                     Something := IntfB.BFunction;
                   end;
         end;
      end;
      

      【讨论】:

      • +1:FPC 好像没有supports 关键字,它也使用CORBA 风格的接口,不是COM,但答案让我走上了搜索的正确轨道。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多