What is the difference between an enum and a set of #define constants?
Short answer
Enum enumerators are typed, scoped (for enum class), visible to the debugger, and grouped as a type, whereas #define constants are untyped preprocessor text substitutions with no scope or type safety and no debug symbol.
Advertisement
AModel answer
Enum enumerators are typed, scoped (for enum class), visible to the debugger, and grouped as a type, whereas #define constants are untyped preprocessor text substitutions with no scope or type safety and no debug symbol. Enums (especially enum class) are strongly preferred over #define for related constants.
enum class State { On, Off }; // typed, debuggable
// vs: #define ON 0 (untyped, no scope)Real-world example
Replacing a group of #define state constants with an enum class adds type checking and debugger names.
Likely follow-ups
- Why are enums safer than #define? | Do enums appear in the debugger?
Related topics