English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

2 answers

You didn't specifically say which language you're using, but since you refer to preprocessor #defines, I assume it's C or C++. In C++, an enum creates a new type, and the named symbols introduced by the type declaration have that type. These symbols are known to the compiler. A #define, on the other hand, introduces a preprocessor token that is replaced (at preprocessor time, before the compiler proper ever sees it) by the body of the definition (which I assume will be a literal integer, since you are comparing #defines to enums). For example:

enum color { red = 1, blue = 2, green = 4 };

creates a type name "color" with values "red", "blue" and "green". These symbols, when used in an arithmetic context, can be converted to the values 1, 2 and 4 (respectively), but they don't have to be. And the names "color", "red", "blue" and "green" can appear in compiler error messages, because the compiler knows about them. By contrast, the declarations

#define RED 1
#define BLUE 2
#define GREEN 4

create preprocessor tokens "RED", "BLUE" and "GREEN" that the preprocessor textually replaces with 1, 2 and 4 (respectively) before the compiler proper ever sees them. The preprocessor tokens as such have no type, and the literals that replace them have type int. This matters because when you use a (named) enum, the fact that it introduces a type means that you can overload on that type. For example, you can write a customized << (stream insertion) operator that will output the color values as words instead of numbers. If you define

std::ostream& operator<<(std::ostream&, color)

appropriately, then the statement

cout << red;

can cause the string "red" to be written to the standard output stream (or even, if you're canny about locales, the strings "rojo" or "rouge" or "rot" or whatever). But if you're using the #define approach, the statement

cout << RED;

cannot write anything but "1" to the output stream, because the preprocessor replaces "RED" with "1" before the compiler sees the statement, so the only version of the << operator that the compiler can possibly choose is the version for type int, which writes the value 1 as "1" (naturally). Put another way, there is no possibility of invoking a type-specific version of operator<< because you haven't created a type. This has implications in other contexts too, like when you're specializing a template (you can specialize on an enum type and have it different than specializing on int).

There are other reasons to use real compiler symbols rather than preprocessor tokens, having to do with scoping, name capture, and such (an enum type and its enumerators can be restricted to a namespace, but preprocessor tokens cannot). In general the enum capability is much safer and more robust than using preprocessor tokens to assign names to numerical constants.

2006-09-13 09:01:28 · answer #1 · answered by logician1989 2 · 1 0

You are GOING to have to start paying attention in your puter classes and stop sleeping when the professor explains all this stuff..
Take notes, read them.. my goodness!

2006-09-13 06:07:24 · answer #2 · answered by chuckufarley2a 6 · 0 0

fedest.com, questions and answers