Sign in to follow this  

int or enum to enum safety [C++]

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

It depends on how you've defined up your enumerations. If you define them so that they are numbered consecutively you can use one if statement to make sure a variable is in the proper range. For example,


enum SomeEnum
{
VALUE_1 = 1,
VALUE_2 = 2,
VALUE_3 = 3,
VALUE_4 = 4
};

if ((someVariable >= VALUE_1) && (someVariable <= VALUE_4))
{
// someVariable is in range
}
else
{
// someVariable is out of range
}




This doesn't work if your enum has gaps, which is sometimes unavoidable.


enum SomeEnum
{
VALUE_1 = 1,
VALUE_2 = 2,
VALUE_3 = 4,
VALUE_4 = 8
};

if ((someVariable >= VALUE_1) && (someVariable <= VALUE_4))
{
// someVariable is between the extreme values but doesn't necessarily
// correspond to an enum value
}
else
{
// someVariable is definitely out of range
}



To make it even easier I usually do something like the following:


enum SomeEnum
{
VALUE_1,
VALUE_2,
VALUE_3,
VALUE_4,
NUMBER_OF_VALUES,
FIRST_VALUE = VALUE_1
};

if ((someVariable >= FIRST_VALUE) && (someVariable < NUMBER_OF_VALUE))
{
// someVariable is in range
}
else
{
// someVariable is out of range
}




In my opinion the biggest advantage to doing it that way is that large enums, you only need one if check to see if a variable is in range, and by abstracting the starting value and the number of enum members if you ever have to change the enumeration you don't have to hunt through your code to change the range checks.

Share this post


Link to post
Share on other sites
Surely you can always cast the enum value to an integer given that the other enum has the same value declared at the same position as the first enum. If you can't guarantee this (and you can't because when extending one of the enums there will be times when you forget to adjust the other one) then you would have to do a big if else if ... cascade to find this out.

As you can already see from those explanations this is something you should not do. If you need to check the containment of one value in two different enums it sounds as if you have choosen a bad design.

Share this post


Link to post
Share on other sites
Quote:
Original post by powell0
It depends on how you've defined up your enumerations. If you define them so that they are numbered consecutively you can use one if statement to make sure a variable is in the proper range.

Well, correct me if I am wrong but you shouldn't use enums to check value ranges. Whenever you need to check values from an enum it should be inside a switch (preferably without a default case). It adds a couple of lines to your code but you will learn to love your compiler when you start adding values to an enum and the compiler tells you all the places in your source code you just forgot to adjust to the newly added value.

Share this post


Link to post
Share on other sites
Thanks for the replies.

Sorry, should have mentioned that the enums have gaps in. I don't care what the value is in this case, just whether it's in the enum. I was mainly just curious to see if there was some basic C++ thing I'd missed, but it appears not.

Share this post


Link to post
Share on other sites
There are a few things you can do to help make it obvious whether or not a particular enum belongs to a set, but there is nothing built into the language that will do it for you. For example, you could put the enum set into a namespace. Or you could embed a common name into all of the enums, such as:
enum eColors
{
eColor_Red,
eColor_Green,
eColor_Blue,
};

The particular notation you use doesn't matter. Some people would not like the 'e' in front of everything that is denoting it's an 'enum', some people would make them all CAPS, etc.

The point is simply to give all of the enums within the set a common base, either through the namesapce, the naming convention, or whatever, to make it obvious which set the particular enum belonged to.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this