Connecting Tech Pros Worldwide Forums | Help | Site Map

How to enum an enum?

Ernst Murnleitner
Guest
 
Posts: n/a
#1: Jul 19 '05
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




Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 19 '05

re: How to enum an enum?


Ernst Murnleitner wrote:
[color=blue]
> 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.[/color]

Can't you solve that with virtual member functions or RTTI?
[color=blue]
> 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[/color]

I don't think there is one.


Victor Bazarov
Guest
 
Posts: n/a
#3: Jul 19 '05

re: How to enum an enum?


"Ernst Murnleitner" <mur-spam@awite.de> wrote...[color=blue]
> 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[/color]
member[color=blue]
> of Family f.
>
> int iNum = 0;
> for(Family e = A; e <= E, e++) // but this does not work, of course[/color]

for(Family e = A; e <= E; e++) // will work if you define
// operator++(int) for 'Family'
// and replace the , with a ;
[color=blue]
> if(p->IsA(e))
> iNum ++;
> cout << "Item is Member of " << iNum << " families" << endl;
> }
>
>
> It seems not to be possible? Is there another elegant solution[/color]

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



Ernst Murnleitner
Guest
 
Posts: n/a
#4: Jul 19 '05

re: How to enum an enum?


Hello,
[color=blue]
> Can't you solve that with virtual member functions or RTTI?[/color]

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



Shane Beasley
Guest
 
Posts: n/a
#5: Jul 19 '05

re: How to enum an enum?


"Ernst Murnleitner" <mur-spam@awite.de> wrote in message news:<bougqa$1itd4c$1@ID-130107.news.uni-berlin.de>...
[color=blue]
> I want to enumerate all values in an enum, e.g.:
> {
> enum Family{A=0, B,C, D = 100, E};[/color]
[snip][color=blue]
> for(Family e = A; e <= E, e++) // but this does not work, of course[/color]
[snip][color=blue]
> It seems not to be possible? Is there another elegant solution[/color]

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
Rolf Magnus
Guest
 
Posts: n/a
#6: Jul 19 '05

re: How to enum an enum?


Ernst Murnleitner wrote:
[color=blue]
> Hello,
>[color=green]
>> Can't you solve that with virtual member functions or RTTI?[/color]
>
> 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?[/color]

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.
[color=blue]
> Now I use a switch/case with all elements of the enum.[/color]

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.
[color=blue]
> Virtual functions: yes, there are some solutions, but I wondered that
> a increment of enums is not possible.Would be nice sometimes.[/color]

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.
[color=blue]
> 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.[/color]

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.

Closed Thread