如何将此结构转换为Delphi?
typedef struct a_config {
union {
struct {
char mode[10];
char name[10];
} dn;
struct {
int r;
} sm;
struct {
int r;
} xo;
} config;
} a_config_t;
答案 0 :(得分:9)
联合等于记录中的case of
构造。并非所有事情都可以1:1翻译,但这可以。
请注意,打包是一个单独的问题。虽然在这种情况下再次可能不是问题。
type
a_config_t = record
config : record
case integer of
0:(dn: record
mode : array[0..9] of ansichar;
name : array[0..9] of ansichar;
end);
1: (sm: record
r: integer;
end);
2: (xo: record
r: integer;
end);
end;
end;
// Delphi has no eq for "struct x" in "struct x {} y" construct, only for the y
a_config = a_config_t;
答案 1 :(得分:5)
C / C ++联合类似于Delphi变体记录。字面翻译是:
type
dn_t = record
mode, name: array [0..9] of AnsiChar;
end;
sm_t = record
r: integer;
end;
xo_t = record
r: integer;
end;
a_config = record
case integer of
0: (dn: dn_t);
1: (sm: sm_t);
2: (xo: xo_t);
end;
阅读文档中有关变体记录的更多信息:http://docwiki.embarcadero.com/RADStudio/en/Structured_Types#Variant_Parts_in_Records
在这种情况下,您可以通过删除嵌套记录来进行更简单的翻译:
type
a_config = record
case integer of
0: (mode, name: array [0..9] of AnsiChar);
1: (r: integer);
end;