![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: enum, enumeration |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Quote:
![]() Although even if you haven't got an IDE with a fancy intelli-sense tool, then its still useful. I believe Scott Meyers has a paragraph in his book, Effective C++, where he praises the benefits of compiler errors as a means to reducing bugs in your program. (Runtime vs Compile-time error checking), and enum variables are checked at compile time, which makes them fairly unique (and highly inflexible). Since enum types give a perma-barrier against a programmer assigning invalid values to an enum object, you can write blocks of code which don't need to be checked at runtime against a programmer accidentally typing inappropriate values for the enum object, the compiler will take care of that. This makes enum types very rigid & restrictive, since values unknown at compile time obviously aren't suitable for enum objects (ie, anything which depends on some runtime calculation, random number, user input, file input, etc). The kinds of values which might be known at compile time, and therefore may be potential candidates for an enum type (IMHO) could include - Cards - Suit & Rank. Number of sides on a dice. Commonly used colours (eg, languages like HTML and Java let you type "Blue" instead of 0000FF ) Anything to do with bit-masks (Enumerated values of 1,2,4,8,16,32, etc) Note - They're also used in conjunction with some almost-voodoo-magic-like template metaprogramming tricks, although that's going into territory far more advanced, by several orders of magnitude, compared to what we've got here |
| |||
| Exactly - protection against "Magic Numbers" ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| |||
| Depending on what you're referring to... yes and no. enum constants are final.. this is a compiler error Code: enum { Hearts, Diamonds, Spades, Clubs };
/* ... */
Hearts = 5; //Bad! Hearts is a constant Code: enum Suit { Hearts, Diamonds, Spades, Clubs };
enum Outcome { Win, Lose, Draw };
/* ... */
Suit var = Diamonds; //OK
var = Lose; //Error! Lose isn't a Suit value
var = 1 // Error! can't assign an int to an enum variable.
var = Hearts //That's ok, var isn't constant, and Hearts is a Suit value.
int n = Lose; //OK - Lose is part of the Outcome enum,
//can implicitly convert to int
n = Clubs; //OK - Clubs can implicitly convert to an int
n = 99 //That's fine too, obviously! |
| The Following User Says Thank You to Bench For This Useful Post: | ||
HelloWorld (08-17-2007) | ||
| |||
| Quote:
In C and C++, there are two ways to define a constant. The first is the most common C way: Code: #define B_FALSE 0 #define TRUE !B_FALSE Code: const int B_FALSE = 0; const int B_TRUE = !B_FALSE; You could use an anonymous enum in a single line to create the TRUE/FALSE constants. If you wanted, you could even create your own BOOLEAN type (BOOL is used in the Win32 API, bool is a C++ keyword and _Bool is a keyword as defined by C99): Code: enum { B_FALSE, B_TRUE }; /* Obviously you should check to see that something is not equal to FALSE rather than checking to see if it is equal to TRUE. */
/* The following creates a BOOLEAN "type" and allows the use of the TRUE and FALSE values. */
/* typedef enum { B_FALSE, B_TRUE } BOOLEAN; */ Oh yeah. People are probably wondering why I prefixed my FALSE and TRUE constants with B_, right? Simply put, it is to avoid clashes with other constants, such as TRUE or FALSE as defined by the Win32 API or some other library. That's another reason why namespaces in C++ are handy. It lets you use a fully-qualified name to avoid such problems. ![]() __________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." -- Bjarne Stroustrup, creator of what is now known as C++ For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that. |
| The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post: | ||
Bench (08-18-2007), HelloWorld (08-18-2007) | ||
| |||
| Quote:
I think one of the scenarios I mentioned was a bit of code which handled dice-rolling. Imagine you're building a roleplaying game system which relies on 3 different types of die - One for movement, one for attacking, and one for defending. Your movement rolls might use a 10-sided die, then your attacking rolls use a 12-sided die, and your defensive rolls use two 6-sided die. You already know, at compile time, that your dice roll function is never going to need any other inputs than 6, 10, or 12, so you may implement it like this Code: enum Dice {
DICE_SIX_SIDED = 6,
DICE_TEN_SIDED = 10,
DICE_TWELVE_SIDED = 12
};
int roll( Dice d )
{
return static_cast<int> ( rand() % d + 1 );
}
void defend()
{
//Get the amount of damage the player is defending against
damage_resisted = roll( DICE_SIX_SIDED ) + roll( DICE_SIX_SIDED );
//calculate the amount of damage the player takes based on the
// damage_resisted dice roll
}
void attack()
{
//Ask the player if he/she would like to attack, get weapon bonuses, etc
int damage = roll( DICE_TEN_SIDED ) + weapon_bonus;
//Do something with the resulting damage calculation
}
void move()
{
int max_squares = roll( DICE_TWELVE_SIDED );
//Ask the player how many squares they'd like to move, based on
// the max_squares diceroll.
} This technique is particularly useful when you're writing some code which someone else is going to use - you can make sure that your user can't break your code by sending a value that it wasn't designed & tested for. |
| |||
| Quote:
There was also another historical issue that C required a constant to specify the size of an array, and not a const variable (something which was changed in C99, but C99 is still unsupported by some compilers). |
![]() |
| Thread Tools | |
| Display Modes | |
| |