Enum
多使用enum
而不是#define
#define RED 0xFF0000
#define GREEN 0x00FF00
#define BLUE 0x0000FF
#define RED 0
#define PURPLE 1
#define BLUE 2
int webby = BLUE;
用enum class
取代enum
Example, bad
void Print_color(int color);
enum Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
enum Product_info { Red = 0, Purple = 1, Blue = 2 };
Web_color webby = Web_color::blue;
Print_color(webby);
Print_color(Product_info::Blue);
Print_color(0);
Example, good
void Print_color(Web_color color);
enum class Web_color { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF };
enum class Product_info { red = 0, purple = 1, blue = 2 };
Print_color(Web_color::red);
Print_color(Product_info::Red);
Print_color(0);
Reference