德尔菲模拟C++枚举?

  • 本文关键字:枚举 C++ 模拟 delphi
  • 更新时间 :
  • 英文 :


德尔菲语言是否与c ++中的此类枚举有类似物

enum EnumOfUChars : unsigned char {
EnumValue1 = 0x10,
EnumValue2 = 0x20
}

更新:
我知道 delphi 中的枚举,问题 - 是否有可能让枚举的值不是 int 而是无符号字符?

enum EnumOfInt {
IntValue = 0x10
};
enum EnumOfUChars : unsigned char {
UCharValue = 0x10
};
int main()
{
printf("Size of IntValue %ldn" , sizeof(IntValue));
printf("Size of UCharValue %ldn" , sizeof(UCharValue));
return 0;
}

输出:

Size of IntValue 4
Size of UCharValue 1

是的,确实如此。您在德尔福的上述类型将是

TYPE
EnumOfUChars = (EnumValue1 = $10, EnumValue2 = $20);

但是,如果 C++ 中的枚举只是 int 值的别名,它们在 Delphi 中是不同的类型。换句话说,而在C++中,您可以执行以下操作:

int c = EnumValue1;

你不能直接在德尔福这样做。以上必须定义为

VAR C : INTEGER = ORD(EnumValue1);

其中"ORD"是德尔菲等价物(但不完全相同(C++中的整数类型转换。

同样,相反的方向:

C++: EnumOfUChars E = 0x22;

这在 Delphi 中无法完成(在不违反枚举类型的情况下(,因为值 $22 不是有效的枚举值。但是,您可以强制它通过:

Delphi: VAR E : EnumOfUChars = EnumOfUChars($22);

如果你想在 Delphi 中对枚举使用二进制值(上面的定义会建议(,你需要使用 SET OF EnumOfUChars 来实现,如下所示:

C++ : EnumOfUChars C = EnumValue1 | EnumValue2;
Delphi: VAR C : SET OF EnumOfUChars = [EnumValue1,EnumValue2];

并测试:

C++ : if (C & EnumValue1)...
Delphi: IF EnumValue1 IN C THEN...

因此,在 Delphi 中,您可以在枚举方面完成相同的操作,但执行此操作的方式与在 C++ 中不同。

根据你需要它的内容,你可以放弃enum,就像这样做:

type
EnumOfUChars = Byte;
const
EnumValue1 = $10;
EnumValue2 = $20;

最新更新