Should you declare enums inside or outside a class if the said enums are only used in the class member functions?
namespace nspace
{
// need to append OC, as this pollutes the current namespace
enum OUTSIDE_CLASS {OC_POINTS, OC_LINES, OC_LINE_LOOP, :::};
enum OTHER_ENUM {OE_POINTS};
class VertexBuffer
{
public:
enum INSIDE_CLASS {POINTS, LINES, LINE_LOOP, :::};
void foo(OUTSIDE_CLASS e);
void bar(INSIDE_CLASS e);
}
};
// usage
nspace::VertexBuffer v;
v.foo(nspae::VB_POINTS);
v.bar(nspace::VertexBuffer::POINTS); // more pedantic
The real goal is to avoid polluting the scope (either global or namespace) and help grouping related values together (works pretty goods with autocompletion in IDE).
With C++11, you can declare strongly typed enums using:
which are necessarily invoked as
MyEnum::Value0(and notValue0).In C++03, you can more or less emulate this with:
But then the type of the enum is
MyEnum::Typewhich is subtly different.The lazy option is to just dump it in a class, but I still favor nesting a scoped enum, even within a class, just to make it clear that those values are not loose but instead are inter-related.