如何获得枚举数 [英] How to get enum count

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

问题描述

如何获得枚举数

我有一个枚举

typedef enum{

DEL_TIME_VALUE    = -1,
DEL_TIMESEC_VALUE = 100,
DEL_TIMEMIN_VALUE = 200,
DEL_TIMEHOUR_VALUE = 300,
DEL_DAY_VALUE      = 1000,
DEL_COUNT_VALUE    = 1000,
....
.....
.....
DEL_END             =90002
}WORKINGTIME;

我如何获得枚举数。

我在下面尝试循环!

for(int i=DEL_TIME_VALUE; i<=DEL_END; i++) {

}

我猜它不是很好!

任何人都可以告诉我如何获得枚举数!在枚举中声明。

can any one tell me how to get enum count! which are declared in enum.

提前致谢!

推荐答案

你不能。

有一种技术可以让你得到枚举数。它看起来像

There is one technique that allows you to get the enum count. It looks like

typedef enum {
    value_one,
    value_two,
    value_three,
    ...
    enum_count
} my_enum;

现在价值 enum_count 是枚举中的值。但是,此技术仅在枚举全部带有隐式值时才有效,其中 value_one 0 value_two 1 ,等等。因此,枚举中的最后一个值总是具有枚举值的计数。在您的情况下,您的枚举常量具有显式值,这些值不是单调递增的。无法从这种类型的枚举中获取计数。即使在你可以得出计数的理论世界中,这也无济于事,因为你无法得出给定枚举常数的值。

Now the value enum_count is the count of values in the enum. However, this technique only works if the enums all carry their implicit value, where value_one is 0, value_two is 1, etc. Because of this, the last value in the enum always has the value of the count of enum values. In your case, your enum constants have explicit values that are not monotonically incrementing. There is no way to derive a count from this type of enum. And even in the theoretical world where you could derive a count, that wouldn't help you because you could not derive the value of a given enum constant.

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

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