I recently came across this:
static enum Response{ NO_ERROR=0, MISSING_DESCRIPTOR, ... };
It compiles and works under Microsoft VS2005. However, I'm not sure what the 'static' modifier is supposed to do. Is it any different from the following?
enum Response { NO_ERROR=0, MISSING_DESCRIPTOR, ... };
As enums are inherently static , there is no need and makes no difference when using static-keyword in enums . If an enum is a member of a class, it is implicitly static. Interfaces may contain member type declarations. A member type declaration in an interface is implicitly static and public.
An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
Yes, enums are effectively static.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
That exact code, with just the ellipsis removed, is not valid C++. You can't use the static
storage class specifier in an enum
declaration; it doesn't make any sense there (only objects, functions, and anonymous unions can be declared static
).
You can, however, declare an enum
and a variable all in one declaration:
static enum Response { NO_ERROR = 0, MISSING_DESCRIPTOR } x;
The static
here applies to x
and it is effectively the same as if you said:
enum Response { NO_ERROR = 0, MISSING_DESCRIPTOR }; static Response x;
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