446,192 Members | 816 Online
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
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" 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" wrote in message news:... 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