By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

How to enum an enum?

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Jul 19 '05 #2

P: n/a
"Ernst Murnleitner" <mu******@awite.de> 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

Jul 19 '05 #3

P: n/a
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

Jul 19 '05 #4

P: n/a
"Ernst Murnleitner" <mu******@awite.de> wrote in message news:<bo*************@ID-130107.news.uni-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
Jul 19 '05 #5

P: n/a
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.

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.