C/C++ Arena

Step 1 of 7

enum class

Plain C enum values leak into the surrounding scope and silently convert to int, so if (team == RED_ALERT) can compare unrelated things. C++'s scoped enum fixes both:

enum class Side { T, CT };
Side s = Side::CT;         // must be qualified
int n = s;                 // error: no implicit conversion
int m = static_cast<int>(s);  // explicit is fine

Combine it with switch: compilers warn (-Wswitch, part of -Wall) when you forget to handle one of the values, which catches bugs whenever someone adds a new enumerator later.

Your turn: write std::string to_string(Weapon w) covering every value, and int price(Weapon w): rifle 2700, sniper 4750, pistol 300.

Next: std::array