枚举问题...... [英] Enum problem...

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

问题描述

有什么方法可以在enum

列表中找到元素的数量?

一般来说,我最后使用最后一个元素来查找总数

元素。例如

typedef enum {

SUN,

MON,

TUE,

EndOfList

}工作日;


但是我面临的情况是我需要知道元素中的元素数量

,没有

EndofList作为最后一个元素。出于向后兼容的原因,我不能更改枚举声明,并添加额外的元素。


如果是数组,我可以使用sizeof(array)/ sizeof(数组元素

类型)....但是这不会在这里工作....


任何人都有解决方案吗?

Rohit

解决方案

Rohit写道:


有没有办法通过它我可以在enum

列表中找到多少元素?



不在代码内,没有。 (你的意思是元素的数量或

最后一个元素的值或元素的最大值?它们都可以是不同的[b $ b] ]。)


有人有解决方案吗?



编写一个程序(或脚本),它读取枚举定义并计算元素数量的元数,然后写出一个C的片段代码为

包含或填写模板中的值。


像往常一样,如果你告诉我们更多关于这个问题的信息,我们或许可以

提供更好的解决方案。


[1] enum可疑

{

iffy = 128,

hinky = 1

};


-

''它改变了未来......它改变了我们。''/ Babylon 5 /

Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN


Rohit写道:


>

是有什么方法可以在

枚举列表中找到多少元素?通常我会在最后使用最后一个元素来查找

元素的总数。例如

typedef enum {

SUN,

MON,

TUE,

EndOfList

}工作日;


但我面临的情况是我需要知道枚举中

元素的数量,没有EndofList作为其最后的

元素。出于向后兼容性原因,我无法更改

枚举声明,并添加额外元素。



如果您在声明中没有使用''=''运算符,那么

的值是最后一个枚举项+ 1是项目数。在上面,

将是:


1 + EndOfList


因此建议写以这样的方式声明

您可以轻松添加到它而无需更改最后一项。例如:


typedef enum {SUN

,MON

,TUE

,EndOfList}工作日;


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。


9月18日,5:33 * pm, Chris Dollin< chris.dol ... @ hp.comwrote:


Rohit写道:


有什么方法可以在enum

列表中找到多少元素?



不在代码范围内,没有。 (你的意思是元素的数量或

最后一个元素的值或元素的最大值?它们都可以是不同的[b $ b] ]。)



好​​的,这里我指的是元素数量。我打算使用

元素的数量。在一个数组声明中,所以数组总是有相同的元素数量,因为有问题的枚举元素。

从你的回答我想它不可能做这个。但是我仍然觉得某些方式应该存在,因为它似乎是一个明显的问题。


>


有人有解决方案吗?



编写一个程序(或脚本),它读取枚举定义并计算元素数量的元数,然后写出一个C的片段代码为

包含或填充模板中的值。



坦率地说,我没有抓住它。读取枚举定义和

计算元素数量????

如何读取枚举定义。它不是一个你可以解析的数组。请详细说明。


像往常一样,如果你告诉我们更多关于这个问题的信息,我们或许能够提供更好的解决方案。


[1] enum可疑

* * {

* * * iffy = 128,

* * * hinky = 1

* *};


-

''它改变了未来..它改变了我们。 '' * * * * * * * /巴比伦5 /


惠普有限公司注册地址:* * * * * * * *该隐路,布拉克内尔,

注册号:690597英格兰* * * * * * * * * * ** * * * * * * Berks RG12 1HN



Rohit


Is there any way by which I can find number of elements in an enum
list?
Generally I use one last element at the end to find out total number
of elements. e.g.
typedef enum{
SUN,
MON,
TUE,
EndOfList
}weekdays;

But I am facing a situation in which I need to know number of elements
in an enum, which do not have
EndofList as its last element. For backward compatibility reasons I
can not change enum declaration which and add extra element.

In case of arrays I can use sizeof(array)/sizeof(array element
type)....But that wont work here....

Anybody has a solution??

Rohit

解决方案

Rohit wrote:

Is there any way by which I can find number of elements in an enum
list?

Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)

Anybody has a solution??

Write a program (or script) which reads the enum definition and counts
the number of elements, then writes out a fragment of C code to
include, or fills values from a template.

As usual, if you tell us more about the problem, we may be able to
provide a better solution.

[1] enum dubious
{
iffy = 128,
hinky = 1
};

--
''It changed the future .. and it changed us.'' /Babylon 5/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


Rohit wrote:

>
Is there any way by which I can find number of elements in an
enum list? Generally I use one last element at the end to find
out total number of elements. e.g.
typedef enum {
SUN,
MON,
TUE,
EndOfList
} weekdays;

But I am facing a situation in which I need to know number of
elements in an enum, which do not have EndofList as its last
element. For backward compatibility reasons I can not change
enum declaration which and add extra element.

If you don''t use the ''='' operator in the declaration, the value of
the last enumerated item + 1 is the count of items. In the above,
that would be:

1 + EndOfList

so it is advisable to write the declaration in such a manner that
you can easily add to it without changing the last item. E.g:

typedef enum { SUN
,MON
,TUE
,EndOfList} weekdays;

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


On Sep 18, 5:33*pm, Chris Dollin <chris.dol...@hp.comwrote:

Rohit wrote:

Is there any way by which I can find number of elements in an enum
list?


Not from within the code, no. (Do you mean "number of elements" or
"value of last element" or "maximum value of elements"? They can all
be different [1].)

OK, here I mean number of elements. I intend to use "number of
elements" in an array declaration, so that array always has same
number of element as there are elements in the enum in question.
From your answer I suppose its not possible to do this. But still I
feel some way should be there, coz it seems to be such an obvious
problem.

>

Anybody has a solution??


Write a program (or script) which reads the enum definition and counts
the number of elements, then writes out a fragment of C code to
include, or fills values from a template.

Frankly speaking I did not catch it. Reading the enum definition and
counting number of elements ????
How can you read a enum definition. Its not an array that you can
parse through. Please elaborate on it.

As usual, if you tell us more about the problem, we may be able to
provide a better solution.

[1] enum dubious
* * {
* * * iffy = 128,
* * * hinky = 1
* * };

--
''It changed the future .. and it changed us.'' * * * * * * */Babylon 5/

Hewlett-Packard Limited registered office: * * * * * * * *Cain Road, Bracknell,
registered no: 690597 England * * * * * * * * * * ** * * * * * *Berks RG12 1HN


Rohit


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

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