same value enum

M

mikron30

Hi,

Is it legal to create two enums with the same value?

typedef enum {
a,
b=a,
c
} save_value_enum;

It passed compilation in Visual Studio 6 and gcc 2.95. But I am not
sure if it is ANSI. I didn't find any saying (for or against) in
Stroustrup.

Thanks,
Miki Zilbershtein
 
J

joe.els

Hi

It is legal in the way that you're allowed to assign values to enums.
Example:
enum MyEnum
{
EA = 0,
EB = -1,
EC = 10,
ED = EC
};

However having duplicate values in your enumerations doesn't make any
logical sense. Enumerations are commonly used as unique constants. If
you take away the uniqueness, then enumerations become less useful.

Also using an enumerations with duplicate values in a switch-statement
won't compile.
Example:
switch (/* some value */)
{
case EA:
...
break;
case EB:
...
break;
case EC:
...
break;
case ED:
...
break;
};

Because EC and ED has the same value of 10, the switch-statement would
break the compilation, giving you some message about duplicate values.


Joe
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top