关于C#:一个类中的枚举,具有显式作用域,在C 11之前?

Enums within a class, with explicit scoping, pre-C++11?

我使用的是VS2010,它没有C 11的强类型枚举。我可以不使用强类型枚举,但同样,我想将枚举保留在类的命名空间之外。

1
2
3
4
5
6
7
8
9
10
11
12
class Example{
    enum Color{
        red,
        green,
        blue
    };

    int Rainbows{
        Color x = red;           // this should be impossible
        Color y = Color::green;  // this is the only way at the enumerations
    }
};

我的问题是,在C 11之前,实现此目标的最佳方法是什么?


1
2
3
4
5
6
7
8
9
10
11
namespace ExampleColor {
   enum Color {
     red,
     green,
     blue
   };
}

class Example {
   int Rainbows{ExampleColor::Color x = ExampleColor::red};
};


我会尝试以下操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Color
{
private:
    int value;

    Color(int newValue)
    {
        value = newValue;
    }

public:
    static Color red;
    static Color green;
    static Color blue;
};

Color Color::red = Color(1);
Color Color::green = Color(2);
Color Color::blue = Color(4);

int main(int argc, char * argv[])
{
    Color color = Color::red;
}