Andre S. Veteran Posted July 9, 2009 Veteran Share Posted July 9, 2009 (edited) I'd like to emulate Java/C# behavior for enums in C++. In Java and C# they are declared and used thus : enum Color { Red, Blue, Green } Color color = Color.Red; There are a few different options : 1) Use a naming convention: enum Color { Color_Red, Color_Blue, Color_Green } Color color = Color_Red; + If you squint, it looks like Java or C# + It's standard - It's just a naming convention - The constants are global + But at least they're prefixed 2) Use a namespace : namespace Color { enum { Red, Blue, Green } } int color = Color::Red; + When you type Color:: you get intellisense for the values + The constants are scoped to a namespace - Must use "int" when declaring a value, meaning there's no type-checking - Declaration looks a bit weird 3) Use Visual Studio non-standard behavior (generates a warning) : enum Color { Red, Blue, Green } Color color = Color::Red; + Perfect behavior, except that - Constants are still global - It won't compile under GCC - Even under msvc it raises a warning 4) Don't bother enum Color { Red, Blue, Green } Color color = Red; + Not bothering is easy - All the constants are global - And there's nothing to tell that they are part of an enum Which would you choose and why? Edited July 9, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
Question
Andre S. Veteran
I'd like to emulate Java/C# behavior for enums in C++. In Java and C# they are declared and used thus :
enum Color { Red, Blue, Green } Color color = Color.Red;There are a few different options :
1) Use a naming convention:
enum Color { Color_Red, Color_Blue, Color_Green } Color color = Color_Red;+ If you squint, it looks like Java or C#
+ It's standard
- It's just a naming convention
- The constants are global
+ But at least they're prefixed
2) Use a namespace :
namespace Color { enum { Red, Blue, Green } } int color = Color::Red;+ When you type Color:: you get intellisense for the values
+ The constants are scoped to a namespace
- Must use "int" when declaring a value, meaning there's no type-checking
- Declaration looks a bit weird
3) Use Visual Studio non-standard behavior (generates a warning) :
enum Color { Red, Blue, Green } Color color = Color::Red;+ Perfect behavior, except that
- Constants are still global
- It won't compile under GCC
- Even under msvc it raises a warning
4) Don't bother
enum Color { Red, Blue, Green } Color color = Red;+ Not bothering is easy
- All the constants are global
- And there's nothing to tell that they are part of an enum
Which would you choose and why?
Edited by Dr_AsikLink to comment
Share on other sites
0 answers to this question
Recommended Posts