Free Pascal 多接口问题

发布于 2024-12-01 18:25:17 字数 762 浏览 2 评论 0原文

这可能会很困难——我在一个对象上使用多个 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

This may prove difficult -- I'm using multiple CORBA interfaces on an object, so it looks somehow like this:

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

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

Now I have a function that takes a variant, and in case that variant is an object, it assumes that object to be a IInterfaceA object:

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

Now once I run that code, and pass a TOtherObject to the function, in line (1) BFunction gets called with forced parameters!

Am I doing something wrong or is it a bug in the compiler? Also, any sane way to circumvent that without changing the class structure?

Full code for a EAccessViolation if anyone wants to try - http://pastebin.com/D7sDpDHx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2024-12-08 18:25:17

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

原来FPC内部不识别CORBA接口。要解决这个问题,需要自己识别它们:

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

然后 as 关键字就起作用了。

Reported this as a bug to the FPC bugtracker - http://bugs.freepascal.org/view.php?id=20076

It turned out that FPC doesn't identify CORBA interfaces internally. To solve the problem one needs to identify them by himself:

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

Then the as keyword will work.

執念 2024-12-08 18:25:17

不确定 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;

Not sure about FreePascal, but in Delphi you would use the supports function to query the interface.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文