什么是非顺序整数c ++枚举的最佳方式 [英] What is the best way for non sequencial integer c++ enums
问题描述
在 C ++枚举模式之后,我已在此处描述,我试图做类似的事情,但这次我想使用的值序列不包括连续的整数。
Following the C++ enum pattern I already described here, I was trying to do a similar thing but this time the sequence of values I want to use is not comprehended of continuous integer numbers.
代码显然是错误的:
class Rotations
{
enum PossibleIndexes
{
ZERO,
PLUS180,
PLUS90,
MINUS90
};
enum PossibleValues
{
ZERO= 0,
PLUS180= 180,
PLUS90= 90,
MINUS90= -90
};
static int Count() { return MINUS90 + 1; }
static PossibleValues Default(){ return ZERO; }
};
,因为两个枚举的内在元素之间会有冲突。
as there will be conflicts between elements inherent of the two enums.
所以我的问题是:什么是实现固定数量的硬编码旋转{0,180,90,-90}的最佳方法,它也有默认和计数功能?
So my question is: What is the best approach to implement a fixed number of hardcoded Rotations{0, 180, 90, -90} which has also a Default and a Count functionality?
推荐答案
由于Visual C ++ 2010编译工具包(不完全符合C ++ 11)的限制,我不得不投降自己到劣等的方法。
Due to the limitations of Visual C++ 2010 Compilation Toolkit (not fully C++11 compliant), I had to surrender myself to inferior approaches.
这篇文章位于 http://stackoverflow.com/a/15961043/ 383779 还建议我一个有趣的方法来获取值。
The post at http://stackoverflow.com/a/15961043/383779 also suggested me an interesting approach for getting the values.
class Rotations
{
public:
typedef enum
{
ZERO= 0,
PLUS180= 180,
PLUS90 = 90,
MINUS90 = -90
}PossibleValues;
static const PossibleValues PossibleValuesCollection(int index) {
static const PossibleValues values[] = { ZERO, PLUS180, PLUS90, MINUS90 };
return values[index];
}
static int Count() { return 4; }
static PossibleValues Default(){ return ZERO; }
};
这篇关于什么是非顺序整数c ++枚举的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!