enum question...

C

Chris Mantoulidis

I can use enums in three ways:

typedef enum { a, b, c, d } ABCD; //this is a TYPE
enum EFGH { e, f, g, h }; //this is a TYPE too
enum { i, j, k, l } IJKL; //this however is a variable...

I believe this happens with structs too, but that's not the point.

Why are there two ways of declaring an enum type??? Isn't one way
enough? Or is the second way more C++-ish?

Thanks in advance,
cmad
 
R

Ron Natalie

Why are there two ways of declaring an enum type??? Isn't one way
enough? Or is the second way more C++-ish?

They are different. The first is an alias of the unnamed enum.

In C++ struct and enum names are themselves types names.
In C they are not, so you either have to repeat the word enum (or
struct) or use the typedef.
 
R

Rolf Magnus

Chris said:
I can use enums in three ways:

typedef enum { a, b, c, d } ABCD; //this is a TYPE
enum EFGH { e, f, g, h }; //this is a TYPE too
enum { i, j, k, l } IJKL; //this however is a variable...

I believe this happens with structs too, but that's not the point.

Why are there two ways of declaring an enum type???

They are different things, though the result is the same. typedef is
there to give an alias name to an existing type.
The first example defines a nameless enum type and gives it an "alias"
name of 'ABCD'. The second one creates an enum with name 'EFGH'. You
can also write:

enum EFGH { e, f, g, h};
typedef EFGH ABCD;

or even combined:

typedef enum EFGH { e, f, g, h} ABCD;

Both of those define an enum named EFGH and give them an alias name of
'ABCD'.

Isn't one way enough?

Well, one of them is just a side effect of the existance of typedef.
There is often more than one way to do the same thing.
 
J

jeffc

Chris Mantoulidis said:
I can use enums in three ways:

typedef enum { a, b, c, d } ABCD; //this is a TYPE
enum EFGH { e, f, g, h }; //this is a TYPE too
enum { i, j, k, l } IJKL; //this however is a variable...

I believe this happens with structs too, but that's not the point.

Why are there two ways of declaring an enum type??? Isn't one way
enough? Or is the second way more C++-ish?

To answer your question specifically, yes the second way is more C++-ish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top