c++ - namespaces for enum types - best practices -


Often, one needs multiple enumerated types. Sometimes, there is a name conflict that has two solutions: Use a namespace, or use the 'bigger' anonymous element names However, there are two possible implementations of the namespace solution: a dummy with nested enum Class or Full Blow Namespace.

I am looking for the professionals and ideas of all three methods.

Example:

  // Many hand-made names collision solutions enum eColors {cRed, cColorBlue, cGreen, cYellow, cColorsEnd}; Enum eFeelings {cangry, cFeelingBlue, cHappy, cFeelingsEnd}; Zero SetpenColor (Constant Echolars c) {Switch (C) {Default: Force (false); break; Case: CRD: // break; Case cColorBlue: // ... // ...}} // (AB) using a class as a namespace class, color {enum e {cRed, cBlue, cGreen, cYellow, cEnd}; }; Class emotions {enum e {cangry, cBlue, cHappy, cEnd}; }; Zero setpancolor (constant color :: ec) {switch (c) {default: emphasis (false); break; Case Color: CRD: // ... break; Case Color: CBLUE: // ... // ...}} // REAL NAME LOCATION? Name space color {enum e {cRed, cBlue, cGreen, cYellow, cEnd}; }; Nom place emotions {enum e {cangry, cBlue, cHappy, cEnd}; }; Zero setpancolor (constant color :: ec) {switch (c) {default: emphasis (false); break; Case Color: CRD: // ... break; Case color: CBLUE: // ... // ...}}  

Benefits from namespace (more than class ) is that when you want using announcements Using

problem is a namespace that the name space can be expanded somewhere else in the code. In a large project, you will not be guaranteed that neither two separate commentators believe that they are called eFeelings

for simple code , I use struct , as you probably would like to make the content public.

If you are doing any of these things, then you are ahead of the curve and probably do not need to check it further. / P>

Appendix for date reply instead:

If you are using C ++ 11 or later, then enum class Enum value within the name of Enum.

With enum class you will lose the underlying conversion and comparison for integer types, but in practice, that can flag your ambiguous or buggy code

< / Div>

Comments