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

Iterating over values in an enum

P: n/a
Hi!

Suppose I want to do some thing like this:

enum X { A, B, C, D };

int main()
{
X a1[] = { A, B, C, D };
int size_a = sizeof(a1) / sizeof(X);
for (int i = 0; i < size_a; ++i)
cout << a1[i] << endl;
}

What I try to illustrate here is that I want to iterate over the values
in the enum X in the order that they are decalred or rather iterate over
the values in order starting with the smallest and eneing with the largest.

Is there a safe way to do this with an enum that will work even if X is
redefined to be something like this:

enum X { D, C, B, A}

or

enum X { A = 0, B = 1, C = 100, D = 42 };

Perhaps it is more appropriate to solve this with a class:

class X
{
public:
static const X A;
static const X B;
static const X C;
static const X D;

const X& MinValue() const;
const X& MaxValue() const;

X& operator++();

private:
X(int i);
int v_;
}

What are your thoughts on this?

:.:: mattias

Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Mattias Brändström wrote:
Hi!

Suppose I want to do some thing like this:

enum X { A, B, C, D };

int main()
{
X a1[] = { A, B, C, D };
int size_a = sizeof(a1) / sizeof(X);
for (int i = 0; i < size_a; ++i)
cout << a1[i] << endl;
}

Then I'd say you have probably been sniffing solvent for too long, or
taking mind-altering drugs.

What I try to illustrate here is that I want to iterate over the values
in the enum X in the order that they are decalred or rather iterate over
the values in order starting with the smallest and eneing with the largest.
Whilst enums have a value that value is in most cases incidental. The
enum gives you a distinct type-labeled-values and it is best dealing
with it as a type-labeled-values (in your case A, B, C, or D). Trying to
then differentiate based on its integer value is simply building
yourself a maintainance problem.

Is there a safe way to do this with an enum that will work even if X is
redefined to be something like this:

enum X { D, C, B, A}

or

enum X { A = 0, B = 1, C = 100, D = 42 };

You are trying to do something to enums that they weren't designed for.
Perhaps it is more appropriate to solve this with a class:

class X
{
public:
static const X A;
static const X B;
static const X C;
static const X D;


Each time you change the enum you have to remember to change the class.

Whats wrong with a vector? I believe they can hold integer values and
you can iterate over them in the order that they were entered.

Jul 22 '05 #2

P: n/a
lilburne wrote:

Whats wrong with a vector? I believe they can hold integer values and
you can iterate over them in the order that they were entered.


Perhaps I should explain in more detail what I want to do.

I want to define a type and also the values that are legal for that
type. In addition to this the values of the type should be ordered, I
should be able to sort them. (I don't know the correct term for this
behaiviour.)

Code that uses said type should not have to be rewritten if the order of
the legal values change.

The more I think about this the more it makes sense to write this as a
class that defines the legal values and the relashionship between them.
Enum is not the way to go.

:.:: mattias

Jul 22 '05 #3

P: n/a


Mattias Brändström wrote:
lilburne wrote:

Whats wrong with a vector? I believe they can hold integer values and
you can iterate over them in the order that they were entered.


Perhaps I should explain in more detail what I want to do.

I want to define a type and also the values that are legal for that
type. In addition to this the values of the type should be ordered, I
should be able to sort them. (I don't know the correct term for this
behaiviour.)

Code that uses said type should not have to be rewritten if the order of
the legal values change.

The more I think about this the more it makes sense to write this as a
class that defines the legal values and the relashionship between them.
Enum is not the way to go.


See Scott Meyers "Effective C++ - Item 46 Prefer compile-time and
link-time errors to runtime errors".
Jul 22 '05 #4

P: n/a
Mattias Brändström wrote:
Hi!

Suppose I want to do some thing like this:

enum X { A, B, C, D };

int main()
{
X a1[] = { A, B, C, D };
int size_a = sizeof(a1) / sizeof(X);
for (int i = 0; i < size_a; ++i)
cout << a1[i] << endl;
}

What I try to illustrate here is that I want to iterate over the values
in the enum X in the order that they are decalred or rather iterate over
the values in order starting with the smallest and eneing with the largest.

Is there a safe way to do this with an enum that will work even if X is
redefined to be something like this:

enum X { D, C, B, A}

or

enum X { A = 0, B = 1, C = 100, D = 42 };

Dan Saks has written many columns covering this issue:
http://www.embedded.com/story/OEG20021114S0035
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.