This is written by someone who has left the company. I can't see any reason to do this and am curious if there's something I am missing.
enum thing_type_e
{
OPTION_A = 0,
OPTION_B,
OPTION_C,
OPTION_D
};
struct thing_type_data_s
{
enum_type_e mVariable;
};
I supposed it's possible he was going to add more to the structure, but after looking at how it is used, I don't think so.
Barring "he was going to add more to the structure," why package a single enum in a struct? Is there some motivation I'm not thinking of?
Update:
As asked in the comments, he used it in this fashion:
void process_thing_type(thing_type_data_s* ParamVariable)
{
local_variable = ParamVariable->mVariable;
...
}
This was originally built with GCC 3.3.5 if it makes any difference.
The enum constants are also known as enumerators. Enum in C# can be declared within or outside class and structs. Enum constants has default values which starts from 0 and incremented to one by one.
4.11 The __packed__ Attribute When attached to an enum definition, it indicates that the smallest integral type should be used. Specifying this attribute for struct and union types is equivalent to specifying the packed attribute on each of the structure or union members.
One of the vital difference between structs and enums is that an enum doesn't exist at run-time. It's only for your benefit when you're read/writing the code. However, instances of structs (and classes) certainly can exist in memory at runtime.
Enumerations make for clearer and more readable code, particularly when meaningful names are used. The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future.
Possibly to enforce some type safety. Old-style enums are implicitly convertible to integral types, and this is not always desirable. Besides this, they are unscoped.
C++11 adds scoped enumerations (or "class" enums) to fix both of these issues.
Here's an example:
void foo(int) {}
int main()
{
foo(OPTION_A); // OK
thing_type_data_s s = { OPTION_A };
foo(s); // Error
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With