In the following code snippet, the Color
enum is declared within the Car
class in order to limit the scope of the enum and to try not to "pollute" the global namespace.
class Car
{
public:
enum Color
{
RED,
BLUE,
WHITE
};
void SetColor( Car::Color color )
{
_color = color;
}
Car::Color GetColor() const
{
return _color;
}
private:
Car::Color _color;
};
(1) Is this a good way to limit the scope of the Color
enum? Or, should I declare it outside of the Car
class, but possibly within its own namespace or struct? I just came across this article today, which advocates the latter and discusses some nice points about enums: http://gamesfromwithin.com/stupid-c-tricks-2-better-enums.
(2) In this example, when working within the class, is it best to code the enum as Car::Color
, or would just Color
suffice? (I assume the former is better, just in case there is another Color
enum declared in the global namespace. That way, at least, we are explicit about the enum to we are referring.)
In general, I always put my enums in a
struct
. I have seen several guidelines including "prefixing".Always thought this looked more like
C
guidelines thanC++
ones (for one because of the abbreviation and also because of the namespaces inC++
).So to limit the scope we now have two alternatives:
I personally tend to use a
struct
because it can be used as parameters for template programming while a namespace cannot be manipulated.Examples of manipulation include:
which returns the number of elements of enum inside the struct
T
:)Nowadays - using C++11 - you can use enum class for this:
AFAII this does exactly what you want.
If you are creating a code library, then I would use namespace. However, you can still only have one Color enum inside that namespace. If you need an enum that might use a common name, but might have different constants for different classes, use your approach.
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.I prefer following approach (code below). It solves the "namespace pollution" problem, but also it is much more typesafe (you can't assign and even compare two different enumerations, or your enumeration with any other built-in types etc).
Usage:
I create macro to facilitate usage:
Usage:
Some references: