-1

Does Delphi language has analog to such enum from c++

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

UPDATE:
I am aware about enums in delphi, question - is it possible to have enum with values not int but unsigned char?

enum EnumOfInt {
    IntValue = 0x10
};

enum EnumOfUChars : unsigned char {
    UCharValue = 0x10
};

int main()
{
    printf("Size of IntValue %ld\n" , sizeof(IntValue));
    printf("Size of UCharValue %ld\n" , sizeof(UCharValue));

    return 0;
}

Output:

Size of IntValue 4
Size of UCharValue 1
  • Does this answer your question? [Delphi and enum](https://stackoverflow.com/questions/2379239/delphi-and-enum) – Miamy Jan 16 '20 at 10:47
  • 1
    Enum values in Delphi are - as I stated in my answer before you changed your question :-) - distinct types. So no, you can't have a distinct type that is a built-in type. Enumerations in Delphi are always their own type. You can, however, emulate the C++ way by using _CONST UCharValue = BYTE($10)_ (where "BYTE" is the Delphi equivalent of _unsigned char_ in C++ ANSI). This will create a constant value of $10 with the intrinsic type "BYTE". Likewise _CONST IntValue = INTEGER($10)_ will create a constant value of $10 with an intrinsic type of INTEGER. SizeOf(IntValue)=4, SizeOf(UCharValue)=1 – HeartWare Jan 16 '20 at 11:06
  • 1
    @HeartWare thank you for the answer and clarifications – Mykhailo Bryzhak Jan 16 '20 at 12:10
  • You can use a compiler directive to set the size of an enum. Again search the docs. This will let you have one byte enumerated types. – David Heffernan Jan 16 '20 at 14:29

2 Answers2

3

Yes, it does. Your above type in Delphi would be

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

However, where enums in C++ are simply aliases for int values, they are distinct types in Delphi. In other words, whereas in C++ you can do:

int c = EnumValue1;

you can't do that - directly - in Delphi. The above would have to be defined as

VAR C : INTEGER = ORD(EnumValue1);

where "ORD" is the Delphi equivalent (but not identical to) an int typecast in C++.

Likewise, the opposite direction:

C++: EnumOfUChars E = 0x22;

This can't be done (without violating the enum type) in Delphi, as the value $22 is not a valid enum value. You can, however, force it through:

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

If you want to use binary values for enums in Delphi (which the above definition would suggest), you'll need to do so using a SET OF EnumOfUChars, as in:

C++ : EnumOfUChars C = EnumValue1 | EnumValue2;

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

and to test:

C++ : if (C & EnumValue1)...

Delphi: IF EnumValue1 IN C THEN...

So, you can accomplish the same in Delphi with respect to enumerations, but the way you do it is different than in C++.

HeartWare
  • 7,464
  • 2
  • 26
  • 30
1

Depending on what you need it for you could drop the enum and just do it like this:

type
  EnumOfUChars = Byte;
const
  EnumValue1 = $10;
  EnumValue2 = $20;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83