Largest enum number
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!
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
};
enum brickType{
iB,
lB,
nlB,
tB,
rB,
nrB,
NUMBER_OF_BRICKS
};
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
};
enum brickType{
iB = 0,
lB,
nlB,
tB,
rB,
nrB,
NUMBER_OF_BRICKS
};
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."
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:
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};
Quote:Original post by JohnBolton
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:or perhapsenum brickType{ iB, lB, nlB, tB, rB, nrB, LAST_BRICK_TYPE = nrB };
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.
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 };
Quote:Original post by doynaxQuote:Original post by JohnBolton
snip
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.
Quote:Original post by SiCrane
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
};
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement