How to enum an enum?

Discussion in 'C++' started by Ernst Murnleitner, Nov 12, 2003.

  1. Hello readers,

    I want to enumerate all values in an enum, e.g.:
    {
    enum Family{A=0, B,C, D = 100, E};

    BaseItem * p = Factory::Get...// BaseItem is the basis of many other items
    // BaseItem has a virtual Function IsA(f) which tests if the item is member
    of Family f.

    int iNum = 0;
    for(Family e = A; e <= E, e++) // but this does not work, of course
    if(p->IsA(e))
    iNum ++;
    cout << "Item is Member of " << iNum << " families" << endl;
    }


    It seems not to be possible? Is there another elegant solution

    Greetings
    Ernst
    Ernst Murnleitner, Nov 12, 2003
    #1
    1. Advertising

  2. Ernst Murnleitner

    Rolf Magnus Guest

    Ernst Murnleitner wrote:

    > Hello readers,
    >
    > I want to enumerate all values in an enum, e.g.:
    > {
    > enum Family{A=0, B,C, D = 100, E};
    >
    > BaseItem * p = Factory::Get...// BaseItem is the basis of many other
    > items // BaseItem has a virtual Function IsA(f) which tests if the
    > item is member of Family f.


    Can't you solve that with virtual member functions or RTTI?

    > int iNum = 0;
    > for(Family e = A; e <= E, e++) // but this does not work, of course
    > if(p->IsA(e))
    > iNum ++;
    > cout << "Item is Member of " << iNum << " families" << endl;
    > }
    >
    >
    > It seems not to be possible? Is there another elegant solution


    I don't think there is one.
    Rolf Magnus, Nov 13, 2003
    #2
    1. Advertising

  3. Hello,

    > Can't you solve that with virtual member functions or RTTI?


    I wanted to avoid RTTI because it runs on an embedded system. How much
    overhead (size) would RTTI cost - is there a rule of thumb?

    Now I use a switch/case with all elements of the enum.

    Virtual functions: yes, there are some solutions, but I wondered that a
    increment of enums is not possible.Would be nice sometimes.

    I have also seen in source codes from others, that they convert enums to int
    and use a for(;;). But this would not work in my case as I have a enum with
    not a monotone increase of the int-value.

    Greetings
    Ernst
    Ernst Murnleitner, Nov 13, 2003
    #3
  4. "Ernst Murnleitner" <> wrote...
    > I want to enumerate all values in an enum, e.g.:
    > {
    > enum Family{A=0, B,C, D = 100, E};
    >
    > BaseItem * p = Factory::Get...// BaseItem is the basis of many other items
    > // BaseItem has a virtual Function IsA(f) which tests if the item is

    member
    > of Family f.
    >
    > int iNum = 0;
    > for(Family e = A; e <= E, e++) // but this does not work, of course


    for(Family e = A; e <= E; e++) // will work if you define
    // operator++(int) for 'Family'
    // and replace the , with a ;

    > if(p->IsA(e))
    > iNum ++;
    > cout << "Item is Member of " << iNum << " families" << endl;
    > }
    >
    >
    > It seems not to be possible? Is there another elegant solution


    Family operator++(Family f, int) {
    if (f == C)
    return D;
    else if (f == E)
    return A; // or not -- this is a wrapping increase
    else
    return Family(f + 1);
    }

    should do it...

    Victor
    Victor Bazarov, Nov 13, 2003
    #4
  5. "Ernst Murnleitner" <> wrote in message news:<bougqa$1itd4c$-berlin.de>...

    > I want to enumerate all values in an enum, e.g.:
    > {
    > enum Family{A=0, B,C, D = 100, E};

    [snip]
    > for(Family e = A; e <= E, e++) // but this does not work, of course

    [snip]
    > It seems not to be possible? Is there another elegant solution


    Enumerators (A, B, C, D, and E) are merely compile-time constants;
    there is no run-time map containing their values or relative order. If
    you need such a map, you'll need to build it yourself via
    cut-and-paste, preprocessor magic, etc.

    - Shane
    Shane Beasley, Nov 13, 2003
    #5
  6. Ernst Murnleitner

    Rolf Magnus Guest

    Ernst Murnleitner wrote:

    > Hello,
    >
    >> Can't you solve that with virtual member functions or RTTI?

    >
    > I wanted to avoid RTTI because it runs on an embedded system. How much
    > overhead (size) would RTTI cost - is there a rule of thumb?


    As soon as you use virtual functions, you typically get overhead of the
    size of one pointer per object. RTTI only works on polymorphic classes,
    so the base class needs at least a virtual function. RTTI itself
    doesn't usually add any per-object overhead, only some overhead per
    class for the typeinfo object and the pointer to it that is usually
    added to the vtable.

    > Now I use a switch/case with all elements of the enum.


    But I guess your objects all have an instance of that enum, so you get a
    small overhead per object, too. However, that overhead is probably
    smaller and you don't get an additional overhead per class.

    > Virtual functions: yes, there are some solutions, but I wondered that
    > a increment of enums is not possible.Would be nice sometimes.


    Since all your enumerators can have any value, that value can't just be
    incremented one by one, so your compiler would have to put in some kind
    of table that contains all the enum values, so it can find the next
    one.

    > I have also seen in source codes from others, that they convert enums
    > to int and use a for(;;). But this would not work in my case as I have
    > a enum with not a monotone increase of the int-value.


    Right. Maybe you could change that somehow? I mean, let the enums be
    continuous and then have an array of the "other" value you need. Then
    you can use your enum value to index into that and get the value you
    need.
    Rolf Magnus, Nov 13, 2003
    #6
    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. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    533
  2. Jerminia
    Replies:
    3
    Views:
    622
    Roedy Green
    Oct 7, 2005
  3. mrhicks
    Replies:
    2
    Views:
    415
    Dave Thompson
    Jun 10, 2004
  4. Randy
    Replies:
    1
    Views:
    516
    David Harmon
    Jan 7, 2006
  5. Randy
    Replies:
    2
    Views:
    360
    Randy Yates
    Jan 8, 2006
Loading...

Share This Page