Iterating over values in an enum

  • Thread starter =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=
  • Start date
?

=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

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 << 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
 
L

lilburne

Mattias said:
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 << 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.
 
?

=?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

lilburne said:
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
 
L

lilburne

Mattias said:
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".
 
T

Thomas Matthews

Mattias said:
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 << 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
 

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,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top