Tech Off Post

Single Post Permalink

View Thread: Enum C# vs C++
  • 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.