Declaring an enum within a class
Solution 1:
If
Color
is something that is specific to justCar
s then that is the way you would limit its scope. If you are going to have anotherColor
enum that other classes use then you might as well make it global (or at least outsideCar
).It makes no difference. If there is a global one then the local one is still used anyway as it is closer to the current scope. Note that if you define those function outside of the class definition then you'll need to explicitly specify
Car::Color
in the function's interface.
Solution 2:
Nowadays - using C++11 - you can use enum class for this:
enum class Color { RED, BLUE, WHITE };
AFAII this does exactly what you want.