   enums and qualified names
  I was digging through some source code on the Game Programming Gems 5 CD which uses a bunch of enums.  VC8.0 was throwing 'nonstandard extension used: enum 'enum' used in qualified name.' warnings, because of code such as:int ret = menuReturn::NONE;With the enum defined asclass menuManager {// original methodstatic enum menuReturn { NONE=0, NEXT, PREV, POPUP, END };};This type of enum definition doesn't allow identifiers to be reused across enums:static enum menuReturn { NONE=0, NEXT, PREV, POPUP, END };static enum menuButton { NONE=0, OK, YES, NO };NONE is found twice and an error is generated.My first solution was to wrap the enums in classes that just function as namespaces:// my first approachclass menuReturn{public:enum { NONE=0, NEXT, PREV, POPUP, END };};But this doesn't allow type safety; you can assign a menuButton enum to a menuReturn.  So I came up with:// better? approachclass menuReturn{public:typedef enum enumType { NONE=0, NEXT, PREV, POPUP, END };};menuReturn::enumType retVal = menuReturn::NEXT;What would you more experienced programmers do in this situation?   
  First of all: forget type safety with enums. They're simply not. You can at any time cast an int into it:enum en { ... };en i = (int)800;What I usually do is prefix the enum members with their abbreviated types, such as to 'bind' the constants to their type:enum eMenuReturn {  MR_NONE,  MR_SOMETHINGELSE,};enum eMenuButton{  MB_NONE,  MB_SOMETHINGELSE,};Greetz,   Illco   ;  I've seen your prefix method used extensively in id's source code, but I think it's extraneous.  Just an opinion though.Regarding type safety, I think it's safe because I must use a C-style cast.  VC8.0 doesn't allow implicit casting of enums.   ;Quote:Original post by IllcoFirst of all: forget type safety with enums. They're simply not. You can at any time cast an int into it:Then why not just use void pointers? I mean you can always explicitly cast them into the right type anyway..   ;Enums, type safety and casting in C++. (and also read the subsequent two sections)As usual, the language is designed to prevent mistakes, not espionage. You can cast an int to an enum, but you do have to cast it, which implies "on your head be it". C++ typing, while not as strong as in (say) Python* or (the usual example) Ada, is quite a bit stronger than in C.* Edit: To be fair, a lot of C++'s weakness in typing has to do with implicit conversions *between primitive numeric types*, and Python sidesteps most of those issues by simply not having all those types. In addition to type "strength" and type "flexibility" (static vs. dynamic), I think it is meaningful to talk at least about a language's type "weight" (very heavy for C++, almost as heavy for Java 1.5 although it was much lighter before, lighter still for Python, almost nonexistant for Perl), and probably several other attributes, too.   ;  Perhaps rather than defining menuReturn and menuButton in the menuManager class, you might define them in the menu and button classes. They probably belong there anyway.[Edited by - JohnBolton on December 10, 2005 1:04:57 PM]