Tech Off Post

Single Post Permalink

View Thread: Enum C# vs C++
  • User profile image
    Sven Groot

    CannotResolveSymbol said:
    No.  According to the C standard, identifiers in an enumerator list must be distinct from all other identifiers declared in the same scope.

    EDIT:  After reading the C++ standard, it should seem that I'm wrong Tongue Out  If you declare your enum as enum class, you should be able to reuse names.  So it would look like this:

    enum class A
    enum class B
    //... where you're using these enums
    B someVariable = B::None;
    B someOtherVariable = B::Value1;
    A yetAnotherVariable = A::None;

    Downsides:  you have to use A:: or B:: to access the enum's values and you can't treat your enumeration's values as an int or bool anymore (so int number = A::None and A enumValue = 1 will not work).

    EDIT2:  enum class doesn't seem to be in the C++ 1998 standard...  your mileage may vary (it may or may not be implemented in Visual Studio; can't try right now).  Guess I should be more careful which document I pull up (I grabbed the Oct. 2008 draft of the C++ standard).  If it's not in VS, your best bet is to prefix your constants with some distinct prefix (like ANone and BNone).
    enum class is a construct of C++/CLI, it defines a managed enum type. It is not available in regular C++.

    I don't believe there is a way to do what you want using standard C++. At least not in C++98 or C99.