什么是非顺序整数c ++枚举的最佳方式 [英] What is the best way for non sequencial integer c++ enums

查看:145
本文介绍了什么是非顺序整数c ++枚举的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里描述了 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.

https://stackoverflow.com/a/15961043/ 383779 还建议我一个有趣的方法来获取价值。

The post at https://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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆