代码之家  ›  专栏  ›  技术社区  ›  Thomas

将C标题转换为Delphi-不透明数据类型

  •  1
  • Thomas  · 技术社区  · 8 年前

    在转换过程中,我遇到了以下C代码:

    /** Opaque data type for the error object.
    */
    typedef struct kError * KErrorRef;
    

    在哪里 kError 已声明?

    这个 conversion tool 由Rudy Velthuis提供,生成以下代码:

    type
      {$EXTERNALSYM KErrorRef}
      KErrorRef = ^kError;
    

    当我尝试编译它时,会收到以下错误消息:

    [dcc32 Error] ukError.pas(50): E2003 Undeclared identifier: 'kError'
    

    转换C代码的适当方式是什么?

    2 回复  |  直到 8 年前
        1
  •  7
  •   Remy Lebeau    8 年前

    kError在哪里申报?

    没有,因为它实际上并不需要。

    在本声明中:

    typedef struct kError * KErrorRef;
    

    struct kError 是不完整的类型,与指针一起使用时允许使用。

    声明是 粗略地 相当于:

    // forward declaration of some as-yet unknown struct type
    struct kError;
    
    // this is OK; compiler knows the size of a pointer, which is not
    // affected by the size of the actual struct being pointed to
    typedef kError *KErrorRef;
    

    Rudy Velthuis提供的转换工具生成此代码

    该工具在此实例中未生成正确的Delphi代码。当处理不完整(正向声明)结构类型的typedef时,它 应该 如果以后没有声明实际的结构类型,则生成更像这样的Delphi代码:

    type
      {$EXTERNALSYM KErrorRef}
      KErrorRef = ^kError;
      {$NODEFINE kError}
      kError = record
      end;
    
        2
  •  6
  •   David Heffernan    8 年前

    我会声明一个空记录,然后声明一个指向它的指针。这给了你打字的安全性。

    type
      KErrorRef = ^kError;
      kError = record
      end;