Including an enum within another enum, possible?

Discussion in 'C Programming' started by mrhicks, May 28, 2004.

  1. mrhicks

    mrhicks Guest

    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
    mrhicks, May 28, 2004
    #1
    1. Advertising

  2. mrhicks

    Case Guest

    mrhicks wrote:
    > 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
    Case, May 28, 2004
    #2
    1. Advertising

  3. On 27 May 2004 16:50:37 -0700, (mrhicks) wrote:

    > 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
    Dave Thompson, Jun 10, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    993
    Jacob
    Jan 12, 2005
  2. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    526
  3. Tony Prichard
    Replies:
    0
    Views:
    718
    Tony Prichard
    Dec 12, 2003
  4. Replies:
    4
    Views:
    660
  5. Ralf Koms
    Replies:
    4
    Views:
    142
    Ralf Koms
    Oct 12, 2004
Loading...

Share This Page