我使用的是VS2010,它没有C++11的强类型枚举,我可以不使用强类型,但我还是希望将枚举排除在我的类的命名空间之外。
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之前,实现这一目标的最好方法是什么?
发布于 2013-03-05 14:04:08
namespace ExampleColor {
enum Color {
red,
green,
blue
};
}
class Example {
int Rainbows{ExampleColor::Color x = ExampleColor::red};
};发布于 2013-03-05 14:14:21
我会尝试下面这样的方法:
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;
}https://stackoverflow.com/questions/15216857
复制相似问题