Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Enum C# vs C++

Back to Forum: Tech Off
  • User profile image
    cro

    In C# this is perfectly legal :

    enum A
    {
        None,
        Value1,
        Value2
    }

    enum B
    {
        None,
        Sometype1,
        Sometype2
    }

    C++ complain that None is already define, so is it possible to accomplish the same thing in C++ ?

    Thanks

  • User profile image
    Cannot​Resolve​Symbol

    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
    {
        None,
        Value1,
        Value2
    }
    
    enum class B
    {
        None,
        Sometype1,
        Sometype2
    }
    
    //... 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).

  • 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
    {
        None,
        Value1,
        Value2
    }
    
    enum class B
    {
        None,
        Sometype1,
        Sometype2
    }
    
    //... 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.

  • User profile image
    Cannot​Resolve​Symbol

    Sven Groot said:
    CannotResolveSymbol said:
    *snip*
    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.
    enum class is planned for C++ 0x; when I pulled up the C++ standard, I grabbed the latest draft instead of the current version...  my mistake.

  • User profile image
    Sven Groot

    CannotResolveSymbol said:
    Sven Groot said:
    *snip*
    enum class is planned for C++ 0x; when I pulled up the C++ standard, I grabbed the latest draft instead of the current version...  my mistake.
    I see, I haven't been keeping up with the developments in C++0x. It's good to know though. Right now there's no way though.

    VC9 already supports it, but as part as C++/CLI so it only works when compiling with /clr, unfortunately. Hopefully VC10 will support it in regular C++ too, which it might since they announced it'd have some C++0x features iirc.

  • User profile image
    cro

    Maybe I can go with something like this until C++ 0x is release and implemented in gcc and VC compiler ?

    class A
    {
    public:
        enum Value { None, Value1, Value2 };
    };

    class B
    {
    public:
        enum Value { None, SomeType1, SomeType2 };
    };

    int main()
    {
        A::Value a = A::None;
        B::Value b = B::None;
    }

  • User profile image
    Sven Groot

    cro said:
    Maybe I can go with something like this until C++ 0x is release and implemented in gcc and VC compiler ?

    class A
    {
    public:
        enum Value { None, Value1, Value2 };
    };

    class B
    {
    public:
        enum Value { None, SomeType1, SomeType2 };
    };

    int main()
    {
        A::Value a = A::None;
        B::Value b = B::None;
    }

    That'd work. With some trickery you would be able to declare variables without using the ::Value bit too, like this:

    class MyEnum
    {
    public:
        enum Value
        {
            None,
            Value1,
            Value2
        };
    
        MyEnum(Value value) : _value(value) { }
    
        operator Value() const
        {
            return _value;
        }
    private:
        Value _value;
    };


    Then you could simply use it as:
    MyEnum x = MyEnum::Value1;


    You'd have to repeat that code for every enum though.

  • User profile image
    cro

    Sven Groot said:
    cro said:
    *snip*

    That'd work. With some trickery you would be able to declare variables without using the ::Value bit too, like this:

    class MyEnum
    {
    public:
        enum Value
        {
            None,
            Value1,
            Value2
        };
    
        MyEnum(Value value) : _value(value) { }
    
        operator Value() const
        {
            return _value;
        }
    private:
        Value _value;
    };


    Then you could simply use it as:
    MyEnum x = MyEnum::Value1;


    You'd have to repeat that code for every enum though.

    Thanks !

  • User profile image
    RichardRudek

    OR, you could try using namespaces:

    namespace A {
    	enum AA{ None, Value1, Value2 };
    }
    
    namespace B {
    	enum BB { None, Sometype1, Sometype2 };
    }
    
    ...
    ...
    	int a   = A::None;
    	int aa  = A::AA::None;
    	int b   = B::None;
    	int bb  = B::BB::None;
    
    EDIT: added some namespace/name permutations. PS: I don't think I like this new editor...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.