# Largest enum number

This topic is 4903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, Im wondering if there is a way of returning the largest/last value in an enum. Eg enum brickType{ iB, lB, nlB, tB, rB, nrB }; where nrB is the largest. I would like not to hardcode the value into my methods you see. Thanks for any help!

##### Share on other sites
Not really, but you can put a value at the end and just call it number.

enum brickType{
iB,
lB,
nlB,
tB,
rB,
nrB,
NUMBER_OF_BRICKS
};

##### Share on other sites
Thanks SiCrane! that makes sense!

##### Share on other sites
Is the enum guaranteed to start at 0? I like SiCrane's method, but I usually add a "= 0" to make sure that the last element has the right value.

enum brickType{
iB = 0,
lB,
nlB,
tB,
rB,
nrB,
NUMBER_OF_BRICKS
};

##### Share on other sites
Yes, they are. Section 7.2 paragraph 2 of the C++ Standard: "If the first enumerator has no corresponding initializer, the value of the corresponding constant is zero."

##### Share on other sites
However, Cacks was asking for the last or largest number. SiCrane's technique returns last+1.

The only way to get the last number is to do it explicitly:
    enum brickType{    iB,    lB,    nlB,    tB,    rB,    nrB,    LAST_BRICK_TYPE = nrB    };
or perhaps
    enum brickType{    iB,    lB,    nlB,    tB,    rB,    nrB,    NUMBER_OF_BRICK_TYPES,    LAST_BRICK_TYPE = NUMBER_OF_BRICK_TYPES-1};

##### Share on other sites
Quote:
 Original post by JohnBoltonHowever, Cacks was asking for the last or largest number. SiCrane's technique returns last+1.The only way to get the last number is to do it explicitly:  enum brickType{ iB, lB, nlB, tB, rB, nrB, LAST_BRICK_TYPE = nrB }; or perhaps  enum brickType{ iB, lB, nlB, tB, rB, nrB, NUMBER_OF_BRICK_TYPES, LAST_BRICK_TYPE = NUMBER_OF_BRICK_TYPES-1};

But that'd would map two enum values to the same value, which is not allowed. You'll have to use some external constant instead.
If you need to have get the last value of the proper enum type you'd have to use a constant enum variable, return it from a function or even use a #define (the only solution I can see if it's needed as a compile time constant too).

Odd.. I could've sworn that I've recieved errors from this before, but it seems to compile just fine in VC at least.

##### Share on other sites
Sometimes you might see something like this:
    enum brickType{        INVALID_BRICK_TYPE = 0,        FIRST_BRICK_TYPE = 1,        iB = FIRST_BRICK_TYPE,        lB,        nlB,        tB,        rB,        nrB,        LAST_BRICK_TYPE = nrB,        NUMBER_OF_BRICKS = LAST_BRICK_TYPE-FIRST_BRICK_TYPE+1    };

##### Share on other sites
Quote:
Original post by doynax
Quote:
 Original post by JohnBoltonsnip

But that'd would map two enum values to the same value, which is not allowed. You'll have to use some external constant instead.
If you need to have get the last value of the proper enum type you'd have to use a constant enum variable, return it from a function or even use a #define (the only solution I can see if it's needed as a compile time constant too).

That's not true. Enumerations can hold constants which have the same value.

Edit: sorry, I see you've corrected this already.

##### Share on other sites
Quote:
 Original post by SiCraneNot really, but you can put a value at the end and just call it number.enum brickType{iB,lB,nlB,tB,rB,nrB,NUMBER_OF_BRICKS};

One more thing. All though this is a convenient technique, it can cause problems because NUMBER_OF_BRICKS is a valid enumeration value even though that is not the intention.

For example, if you have a function like
    SetBrickType( brickType type ) { ... }
then the compiler will not catch this mistake:
    SetBrickType( NUMBER_OF_BRICKS );
So everywhere in your code, you have to make sure that the value of the enumeration is not NUMBER_OF_BRICKS. In addition, GCC has the nice feature where it will generate a warning if all the enumeration values are not accounted for in a switch statement. In the following code you will get a warning because NUMBER_OF_BRICKS was not handled in the switch statement even though that should never happen.
    switch( brick_type )    {        case iB: ...; break;        case lB: ...; break;        case nlB: ...; break;        case tB: ...; break;        case rB: ...; break;        case nrB: ...; break;    }
Overall though, the convenience usually outweighs these problems. To get around the problems you would do this (or something similar):
    enum brickType{        iB,        lB,        nlB,        tB,        rB,        nrB    };    int const NUMBER_OF_BRICKS = nrB+1;

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634102
• Total Posts
3015534
×