Including an enum within another enum, possible?

M

mrhicks

Hello all,

I have a question about enumerations. Within some requirements data
passed back a certain bit field is defined by three bits then in
another section the bit field is defined as 4 bits. The second set is
a superset of the first set. The enumeration look as following...

enum SET1 { AutoOffState = 0x00, AutoOneState = 0x01,
TrippedOffState = 0x02, TrippedOnState = 0x03,
LockedOffState = 0x04, LockedOnState = 0x05,
CollardOffState = 0x06, CollardOnState = 0x07
};

enum SET2 { AutoOffState = 0x00, AutoOneState = 0x01,
TrippedOffState = 0x02, TrippedOnState = 0x03,
LockedOffState = 0x04, LockedOnState = 0x05,
CollardOffState = 0x06, CollardOnState = 0x07,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

Note: I know the names with the enumeration can not be the same as
it will generate a compile error.

There are a lot of returned data bit fields that are similar, but
the values are the same. I was wondering if there is some way to
include a perviously defined enumeration within another enumeration
like

enum SET2 { enum SET1,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

But this does work. I also tried the following


enum SET2 { AutoOffState, AutoOneState,
TrippedOffState, TrippedOnState,
LockedOffState, LockedOnState,
CollardOffState, CollardOnState,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

But this will not work either as the names will collide and generate
a compile error. Is there a clean way of adding an already defined
enumeration within another enumeration declaration? What is the best
way to handle this? I am just trying to reduce errors and tpying as I
have a lot of fields that overlap. Yes, cut and paste works great, but
if the value changes I would like it to propagate down if possible.
Thanks!!!


Mark
 
C

Case

mrhicks said:
Hello all,

I have a question about enumerations. Within some requirements data
passed back a certain bit field is defined by three bits then in
another section the bit field is defined as 4 bits. The second set is
a superset of the first set. The enumeration look as following...

enum SET1 { AutoOffState = 0x00, AutoOneState = 0x01,
TrippedOffState = 0x02, TrippedOnState = 0x03,
LockedOffState = 0x04, LockedOnState = 0x05,
CollardOffState = 0x06, CollardOnState = 0x07
};

enum SET2 { AutoOffState = 0x00, AutoOneState = 0x01,
TrippedOffState = 0x02, TrippedOnState = 0x03,
LockedOffState = 0x04, LockedOnState = 0x05,
CollardOffState = 0x06, CollardOnState = 0x07,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

Note: I know the names with the enumeration can not be the same as
it will generate a compile error.

There are a lot of returned data bit fields that are similar, but
the values are the same. I was wondering if there is some way to
include a perviously defined enumeration within another enumeration
like

enum SET2 { enum SET1,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

But this does work. I also tried the following

'this does *not* work' you mean of course.
enum SET2 { AutoOffState, AutoOneState,
TrippedOffState, TrippedOnState,
LockedOffState, LockedOnState,
CollardOffState, CollardOnState,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

But this will not work either as the names will collide and generate
a compile error. Is there a clean way of adding an already defined
enumeration within another enumeration declaration? What is the best
way to handle this? I am just trying to reduce errors and tpying as I
have a lot of fields that overlap. Yes, cut and paste works great, but
if the value changes I would like it to propagate down if possible.

Why don't you create an enum or alternatively a group of defines
that covers the superset of all enum values.

#define AUTO_OFF_STATE 0x00
....

enum SET {
AutoOffState = AUTO_OFF_STATE,
...
};

enum SET2 {
AutoOffState = AUTO_OFF_STATE,
...
};

HTH

Case
 
D

Dave Thompson

Hello all,

I have a question about enumerations. Within some requirements data
passed back a certain bit field is defined by three bits then in
another section the bit field is defined as 4 bits. The second set is
a superset of the first set. The enumeration look as following...

enum SET1 { AutoOffState = 0x00, AutoOneState = 0x01,
<rest snipped>

BTW- AutoOne not On as in the others?
There are a lot of returned data bit fields that are similar, but
the values are the same. I was wondering if there is some way to
include a perviously defined enumeration within another enumeration
like

enum SET2 { enum SET1,
NoComm = 0x09, SwitchFault = 0x0A,
NotInstalled = 0x0B
};

But this does [not] work. I also tried <snip>
Is there a clean way of adding an already defined
enumeration within another enumeration declaration? What is the best
way to handle this? I am just trying to reduce errors and tpying as I
have a lot of fields that overlap. Yes, cut and paste works great, but
if the value changes I would like it to propagate down if possible.

Not really. In C, but not C++, enum values are not really "in" any one
type, they are *all* just ints. And an enum { list } type is some
(otherwise available) integer type, sufficient to cover the range of
values in its list; many compilers just make all enum types 'int',
since that is always sufficient, and simplest.

The two approaches are:

1) define (a type "containing") all the values, and then use subranges
of it, sort of Pascal or Ada style:

enum Full { Off, On, Starting, Stopping, Unknown, Maybe };
/* optionally add typedef ... whatever */
enum Brief { Brief_min = Off, Brief_max = Stopping }; /* ditto */

2) define a sublist and then "append" to it, two ways:

enum Brief { Off, On, Starting, Stopping };
enum Full { Full_before = Stopping, Unknown, Maybe };
/* or */
enum Brief { Off, On, Starting, Stopping, Brief_next };
enum Full { Unknown = Brief_next, Maybe };

- David.Thompson1 at worldnet.att.net
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top