How to enum an enum?

E

Ernst Murnleitner

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
 
R

Rolf Magnus

Ernst said:
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.
 
E

Ernst Murnleitner

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
 
V

Victor Bazarov

Ernst Murnleitner said:
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
 
S

Shane Beasley

Ernst Murnleitner said:
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
 
R

Rolf Magnus

Ernst said:
Hello,


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.
 

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