• C

enum (high, low)


if I define:
enum COLORS {
    red,
    yellow
    blue,
    green
}
How to find programaticaly (in C or C++) what is the lowest and the highest member of COLOR? Something like Delphi Pascal's function High, which returns "green" and function Low which returns "red" for the above enum.
yuhooAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

marty5Commented:
Hi,

In C, I do not believe that you can.

This is simply because an enum is basically an
int with the enums simply acting as if you had
given them numbers via #defines.

The main advantage of enums besides readability
is that values can come up literally in a debugger.

I am not a C++ genius, but I do not think you can
do it there either.

The difference in C++ from C with enums is that
you can assign an enum variable an integer  number
in C, but not in C++ where you have to use a symbolic
constant in the assignment statement.  I do not
know of a way to do you want but you can get tricky
by always defining a special terminator name such
as colors_last at the end of your enum definition.  Then
you could test its value.

Marty


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
The terminators are the only way I know of in C++.
enum's have another advantage, at least in C++; any parameter that is expected to be of a specific enum type is checked for that type. So

void f(COLOR parm);

void g()
{
  f(0); // is an error
  f(COLOR::red); // is ok
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.