Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the max value in ENUM

Status
Not open for further replies.

Lorey

Programmer
Feb 16, 2003
88
SG

Hi!

I have an enum type like:

enum callnumbers
{ one = 1
two = 2
three = 3 }

but this enum list has a tendency to be updated like adding

enum callnumbers
{ one = 1
two = 2
three = 3
four = 4}

my question is how to dynamically gets the maximum value in the enum list.
 
Typically, I include a maximum
Code:
enum {
  one,
  two,
  three,
  max // insert additions before this
};

But if you go in for actually numbering the enums yourself, and put in some perverse ordering like this, then you're on your own.
Code:
enum {
  three = 3,
  two = 2,
  one = 1,
  max // (will be 2 in this case)
};

--
 
There is no dedicated support for this in C++.

One tricky thing would be to make an array of them and then get the size by casting the sizeof array to the sizeof the first element in the palce you need. Then if the numbers are consecuve, this may help, if nto the approach is useless.

HTH,

s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top