开发者

Are circular unit references in the uses list possible in delphi?

I am translating some C header files to Delphi.

In those header files, two files share their struct with each other.

When I tried this in Delphi, it gave me circular reference error.

So I am currently writing both header's translation in single .pas file.

Is there any other way to g开发者_JS百科et around this problem?

This is a small example.

The actual header files are more complicated :\

==File1.h==

struct a
{
int data;
}
int compare(struct a,struct b);

==File2.h==

struct b
{
int data;
}
int compare(struct A,struct b);

==File1.pas==

uses File2;
type
  a = packed record
    data: Integer;
  end;

compare = function(d1: a; d2: b): Integer; cdecl;

==File2.pas==

uses File1;
type
  b = packed record
    data: Integer;
  end;

compare = function(d1: a; d2: b): Integer; cdecl;


As Eugene points out circular interface unit references are not possible in Delphi. There are a few possible solutions:

  • Move the shared struct to a third unit and let the other two units include this unit.
  • Move one of the unit references to the implementation section (if this is possible).
  • Keep all in one file as you mention

Note that the fact that Delphi forces you to think about circular references is not a bad thing IMHO. In many cases, these circular references draw your attention to a flaw in your design.


Yes, you can't make the units reference each other in "interface" section. Move "uses" clause of one file to implementation section. This is a limitation of pascal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜