循环枚举值 [英] Loop on enumeration values

查看:134
本文介绍了循环枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个枚举定义,它是多么可怕,或者是完全可以接受的。文字的值是默认值。分配的值没有任何意义,没有任何意义,并且在将来添加的任何文字的值也将没有任何意义。它只是被定义来限制允许的值,并使事情更容易遵循。因此,值将始终从0开始并增加1.



我可以设置一个循环,如下所示:

 枚举MyEnum 
{
value1,
value2,
value3,
maxValue
}

for(MyEnum i = value1; i< maxValue; i = static_cast< MyEnum>(i + 1)){}


解决方案

就我而言,这很好。我相信一些纯粹的某处在某个地方会怪异,但就语言规范而言,该代码将正常工作,所以如果它让你的生活更容易,你应该随时去。


How awful is it - or is it perfectly acceptable - to index a loop on an enumeration?

I have an enumeration defined. The values of the literals are default values. The assigned values do not have any significance, will not have any significance, and the values of any literals added in the future will also not have any significance. It's just defined to limit the allowed values and to make things easier to follow. Therefore the values will always start at 0 and increase by 1.

Can I set up a loop like so:

enum MyEnum
{
    value1,
    value2,
    value3,
    maxValue
}

for(MyEnum i = value1; i < maxValue; i = static_cast<MyEnum>(i+1)){}

解决方案

As far as I'm concerned, that's just fine. I'm sure some purist out there somewhere will freak out, but as far as the language spec is concerned, that code will work correctly, so you should feel free to go for it if it makes your life easier.

这篇关于循环枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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