7

我有两种类型。一种 A 型和一种 B 型。问题 A 型包含 B 型,B 型包含 A 型。这样的事情是行不通的:

  type
    typeA = record
       test1 : typeB;
    end;
  type
    typeB = record
       test2 : typeA;
    end;

编辑: 那不是我的设计。我将包含此类结构的 C 头文件(以访问 DLL)转换为 delphi。

编辑 2: “C++ 结构是 AFAIR 类的另一个名称。而且必须有指针,而不是值本身。 – Arioch '1 分钟前”是的,你是对的,它是一个指向类型的指针:

我在那里定义:

test1 : ^typeB;

那会起作用吗?

test1 : Pointer;

Edit3: C结构:

/* DLPDFPAGE */
typedef struct dlpdfpage
{
    CosObj              Page;
    CosObj      PrintSelect;
    ASFixedRect         PageBBox;
    ASFixedRect         ContentBBox;
    struct dlpdfpage    *Next;
    PDRotate            Angle;
    struct dlpdfdoc     *Doc;
    DLPDFSTREAM         *Content;
    long                PageNumber;
    char                Complete;
    char                FontSubstituted;
    char                FontMM;
    char                FontBad;
} DLPDFPAGE;


/* DLPDFDOC */
typedef struct dlpdfdoc
{
    DLPDFINSTANCE       *dliInstance;
    PDDoc               pdDoc;
    CosDoc              cosDoc;
    DLPDFOUTLINE        *Outlines;
    char                *PDFFileName;
    char                *PDFPostFileName;
    DLPOS               LastPageEnd;
    DLPOS               BeforeDef;
    ASFixedRect         DocBBox;
    long                PageCount;
    long                PageTreeWidth;
    long                PageTreeDepth;
    long                PageTreeDepthUsed;
    DLPDFPAGETREEARRAY  *AllPages;
    DLPDFFONTLIST       *AllFonts;
    DLPDFFORMLIST       *AllForms;
    DLPDFFORMLIST       *AllColors;
    DLPDFIMAGELIST      *AllImages;
    DLPDFSPOTCOLORLIST  *AllSpotColors;
    DLPDFSPOTCOLORLIST  *AllPatterns;
    DLPDFEXTGSTATELIST  *AllExtGStates;
    DLPDFPAGE           *PageList;
    DLPDFPAGE           *LastPage;
    DLPDFDEST           *DeferedDests;
    DLPDFSIGNATURE      *signatureHolder;
    struct dlpdfacroform *AcroFormBase;
    CosObj              PatternColorObj,
                        PatternColorRGBObj,
                        PatternColorCMYKObj,
                        PatternColorGrayObj,
            PrintSelect,
            PrintSelectCriteria;
    CosObj      IdentH, IdentV;
    ASAtom              DocumentEncoding;
    long                FontCount;
    long                FormCount;
    long                PatCount;
    long                ImageCount;
    char                Compress;
    char                Linearize;
    char                PageTreeComplete;
    char                EmbedFonts;
    char                PatternColorsDefined;
    char                MakeThumbNails;
    ASBool              psSevenBitSafe;
    ASInt32             EncryptKeyByteCount;

    char                condenseResDicts;
    CosObj              resourceDict;  

    ASInt16             pdfMajorVer;    
    ASInt16             pdfMinorVer;    

    DLPDFINCLUDEDRES    *InclRes;       

    DLPDFSPOTCOLORLIST  *AllShadings;
    long                ShadeCount;

} DLPDFDOC;
4

3 回答 3

14

您误解了那些 C 结构代表什么。那是因为 arecord是一个值类型:它存储在你声明变量的地方。所以让我们做几层递归声明,你就会明白我的意思了;假设这两个结构并不完全相同:

type
  TA = record
     test1 : TB;
     SomethingElseFromA: Byte;
  end;

  TB = record
     test2 : TA;
     SomethingElseFromB: Byte;
  end;   

结构TA可以重写为:

type
  TA = record
    // Replaced test1 : TB with the actual content of TB, because that's
    // what a record means.
    test1_test2: TA;
    test1_SomethingElseFromB: Byte;

    SomethingElseFromA: Byte;
  end;

当然,我们现在已经很好地将 self 递归包含到TA记录中,类似于:

  TA = record
    // Replaces test1_test: TA
    test1_test2: TA; // Oops, still not fixed, need to do it again...
    test1_SomethingElseFromB: Byte;
    SomethingElseFromA: Byte;

    test1_SomethingElseFromB: Byte;
    SomethingElseFromA: Byte;
  end;

您可能想使用引用类型来获得看起来相似但并不相似的东西。引用类型始终是指针,因此它是固定大小的;编译器可以毫无问题地分配它。这将是有效的,使用指向记录的指针:

type
  pTypeB = ^typeB;
  pTypeA = ^typeA;

  typeA = record
     test1 : pTypeB;
  end;

  typeB = record
     test2 : pTypeA;
  end;

或者,您可以使用类;出于同样的原因,类是引用类型;它们的工作方式与指针相同。当您声明一个指针类型的变量时,编译器会分配SizeOf(Pointer)字节。


既然你已经发布了 C 结构,我可以说它们太长了,我无法尝试完整的翻译,但我可以提出一些建议: 你应该在一个Type块中声明所有类型;不要Type在每个类型声明之前编写。这允许您在记录类型之前创建指针类型,如下所示:

Type
  PMyRecord = ^TMyRecord;

  // Somewhere in the same Type block
  TMyRecord = record
  end;

对于需要指向记录的指针的每种类型,首先在Type关键字之后声明指针,这样更简单。接下来,您需要识别 C 指针。如果*数据类型的名称和字段的名称之间有一个,那就是一个指针。这通常是这样写的:

int *PointerToSomeInt;

但这些同样有效:

int * PointerToSomeInt;
int* VarName1, * VarName1, * VarName3; // Three pointers to integer.

最后,您需要处理对齐问题。如果可以,检查 C 端结构的大小,然后检查 Delphi 端的大小:你应该得到相同的大小。如果你不这样做,你应该{$ALIGN}在你的结构声明之前尝试几个随机的编译器指令并重复,直到你找到正确的对齐方式。如果所有其他方法都失败了,您需要找出问题所在(哪些字段在 Delphi 端的对齐方式不同)并放入一些对齐字节来人为地修复它。

于 2013-01-21T11:38:11.003 回答
11

也许最好的解决方案是重新考虑设计。但您可能也对所谓的类前向声明​​感兴趣:

type
  TTypeB = class;

  TTypeA = class
    test: TTypeB;
  end;

  TTypeB = class
    test: TTypeA;
  end;      

原文如此!这仅适用于课程,不适用于记录。

于 2013-01-21T11:20:12.547 回答
1

您展示的 C 代码的 Delphi 翻译如下所示:

type
  DLPDFDOC = record; // forward declaration

  { DLPDFPAGE }
  DLPDFPAGE = record
    Page: CosObj;
    PrintSelect: CosObj;
    PageBBox: ASFixedRect;
    ContentBBox: ASFixedRect;
    Next: ^DLPDFPAGE;
    Angle: PDRotate;
    Doc: ^DLPDFDOC;
    Content: ^DLPDFSTREAM;
    PageNumber: Longint;
    Complete: AnsiChar;
    FontSubstituted: AnsiChar;
    FontMM: AnsiChar;
    FontBad: AnsiChar;
  end;

  { DLPDFDOC }
  DLPDFDOC = record
    dliInstance: ^DLPDFINSTANCE;
    pdDoc: PDDoc;
    cosDoc: CosDoc;
    Outlines: ^DLPDFOUTLINE;
    PDFFileName: PAnsiChar;
    PDFPostFileName: PAnsiChar;
    LastPageEnd: DLPOS;
    BeforeDef: DLPOS;
    DocBBox: ASFixedRect;
    PageCount: Longint;
    PageTreeWidth: Longint;
    PageTreeDepth: Longint;
    PageTreeDepthUsed: Longint;
    AllPages: ^DLPDFPAGETREEARRAY;
    AllFonts: ^DLPDFFONTLIST;
    AllForms: ^DLPDFFORMLIST;
    AllColors: ^DLPDFFORMLIST;
    AllImages: ^DLPDFIMAGELIST;
    AllSpotColors: ^DLPDFSPOTCOLORLIST;
    AllPatterns: ^DLPDFSPOTCOLORLIST;
    AllExtGStates: ^DLPDFEXTGSTATELIST;
    PageList: ^DLPDFPAGE;
    LastPage: ^DLPDFPAGE;
    DeferedDests: ^DLPDFDEST;
    signatureHolder: ^DLPDFSIGNATURE;
    AcroFormBase: ^DLPDFACROFORM;
    PatternColorObj: CosObj;
    PatternColorRGBObj: CosObj;
    PatternColorCMYKObj: CosObj;
    PatternColorGrayObj: CosObj;
    PrintSelect: CosObj;
    PrintSelectCriteria: CosObj;
    IdentH: CosObj;
    IdentV: CosObj;
    DocumentEncoding: ASAtom;
    FontCount: Longint;
    FormCount: Longint;
    PatCount: Longint;
    ImageCount: Longint;
    Compress: AnsiChar;
    Linearize: AnsiChar;
    PageTreeComplete: AnsiChar;
    EmbedFonts: AnsiChar;
    PatternColorsDefined: AnsiChar;
    MakeThumbNails: AnsiChar;
    psSevenBitSafe: ASBool;
    EncryptKeyByteCount: ASInt32;
    condenseResDicts: AnsiChar;
    resourceDict: CosObj;  
    pdfMajorVer: ASInt16;    
    pdfMinorVer: ASInt16;    
    InclRes: ^DLPDFINCLUDEDRES;       
    AllShadings: ^DLPDFSPOTCOLORLIST;
    ShadeCount: Longint;
  end;
于 2013-01-21T20:30:36.717 回答