The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


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: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #11 (permalink)  
Old 08-16-2007, 06:55 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by ccoonen View Post
If User.UserType = (Then intellisense will popup and tell you your options) - no need to remember what it is and it's reliable because it now knows what it can be and you can just select the options from a list.
That indeed is very handy if your IDE can do it

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

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #12 (permalink)  
Old 08-16-2007, 11:56 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 317
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Exactly - protection against "Magic Numbers"
Reply With Quote
  #13 (permalink)  
Old 08-16-2007, 03:40 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
Originally Posted by ccoonen View Post
Exactly - protection against "Magic Numbers"
are they final?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #14 (permalink)  
Old 08-16-2007, 04:31 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
are they final?
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
although enum variables follow the same rules as other variables - with the exception that you can only assign one of the given enum values of that type to it

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!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (08-17-2007)
  #15 (permalink)  
Old 08-17-2007, 08:53 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Why do we want to use ENUM those are not constant instead of just a regular variables? since the value of it is always changed, then I guess there's no difference between ENUM and variables if the ENUM is not constant right?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #16 (permalink)  
Old 08-17-2007, 09:26 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Quote:
Originally Posted by HelloWorld View Post
Why do we want to use ENUM those are not constant instead of just a regular variables? since the value of it is always changed, then I guess there's no difference between ENUM and variables if the ENUM is not constant right?
I think you missed the point. :-P

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
The second is what is more commonly used in C++:
Code:
const int B_FALSE = 0;
const int B_TRUE = !B_FALSE;
(I'm not sure about why #define is preferred in C since both create read-only values. The only difference is that any constant created using #define is replaced globally by the preprocessor with the value that the constant is defined as (e.g. any FALSE in the code would be replaced with a 0). With the const keyword, memory is reserved for it to be an unchanging value. In addition, the use of the const keyword still requires a storage type to be defined for a variable, meaning it is more type-safe, which is what C++ helps to correct about C.)

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; */
I forget what is under the hoods of the bool and _Bool keywords, but the idea is little more than some additional type-checking on an int, I would imagine.

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.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to rpgfan3233 For This Useful Post:
Bench (08-18-2007), HelloWorld (08-18-2007)
  #17 (permalink)  
Old 08-18-2007, 06:14 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Why do we want to use ENUM those are not constant instead of just a regular variables? since the value of it is always changed, then I guess there's no difference between ENUM and variables if the ENUM is not constant right?
Like I said before, its generally used when you want compile-time checking as opposed to runtime checking - because enum types deliberately restrict what values an enum variable can hold.

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.
}
Of course, the enum doesn't stop you using a 12-sided dice when you actually needed a 10-sided dice, but it does stop you, the programmer, from rolling with a completely invalid dice, such as 9999-sided, or -2-sided.

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.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #18 (permalink)  
Old 08-18-2007, 06:38 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by rpgfan3233 View Post
I'm not sure about why #define is preferred in C since both create read-only values
I think its partially historical. the const keyword didn't appear in the original version of K&R 'C' (at least, it wasn't mentioned in the 1979 book 'The C Programming Language'). despite the fact that 'const' appeared in the first ANSI standard of the language, C89, there were, inevitably a lot of compilers which didn't recognise the const keyword til much later, so #define was preferred for the sake of cross-platform compatibility.

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).

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 01:58 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50