Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "static enum" mean in C++?

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,     ... }; 
like image 592
Marcin K Avatar asked Feb 11 '11 16:02

Marcin K


People also ask

Why is enum static?

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.

Can you have a static enum?

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).

Are enums static by default?

Yes, enums are effectively static.

What is enum and example?

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.


1 Answers

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; 
like image 188
James McNellis Avatar answered Oct 03 '22 04:10

James McNellis