View Single Post
  #7   Spotlight this post!  
Unread 04-29-2005, 05:27 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

Quote:
Originally Posted by dhoizner
Dave, I am unfamiliar with the 'enum' command, what does it do? Assign values to the identities inside, ascending from the value assigned to the first identity?
The enum represents an "enumeration" or "enumerated list". Your interpretation is correct. It creates a list of constants to be used later in your code. The constants are numbered by adding one to the previous value. You can also change the numbering scheme whenever you want to by doing the following
Code:
//This is equivalent to
#define STEP_A  0
#define STEP_B  (STEP_A + 1)
#define STEP_C  5
#define STEP_D  (STEP_C+1)

enum
{
  STEP_A = 0,
  STEP_B,
  STEP_C = 5,
  STEP_D,
}
One of the benefits is that you don't have to define the values for the constants as they will autoincrement. If used properly, you won't have to worry about accidentally reusing numbers. You will also be able to renumber the constants simply by rearranging them in the enum structure. Here's an example...


Say that you have 3 steps to a process defined as #defines
Code:
#define STEP_A  1
#define STEP_B  2
#define STEP_C  3
You have your constants and they work just fine. During testing, you realize that you need to add STEP_D before STEP_C. You make the following change to your code
Code:
#define STEP_A  1
#define STEP_B  2
#define STEP_C  3
#define STEP_D  4
While there is nothing wrong with this, your constants don't reflect the fact that STEP_D occurs before STEP_C.

If your original code was written with enums
Code:
enum
{
  STEP_A = 1,
  STEP_B,
  STEP_C
};
You could easilly add STEP_D and have the step order make sense
Code:
enum
{
  STEP_A = 1,
  STEP_B,
  STEP_D,
  STEP_C
};
Unlike a #define, these constants will reside in the memory space of your program, as opposed to being substituded in pre-compilation. This may be a downside if you have limited memory or speed.

In the context that I used in my original response, #defines would have been just fine, I just chose to go with the enum because I wasn't too concerned with the values that the constants took on....I was more interested in having unique constants.

You can find some more info about enums here