为什么C中没有枚举的增量运算符? [英] Why is there no increment operator for enums in C?

查看:56
本文介绍了为什么C中没有枚举的增量运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C中没有枚举的增量运算符?即使相应的整数值是用户定义的,对我来说用 ++ 迭代到下一个成员也是很有意义的.

Why is there no increment operator for enums in C? Even if the corresponding integer values are user-defined it does make perfectly sense to me to iterate with ++ to the next member.

更清晰

typedef enum myenum t_myEnum;
enum myenum {
    eMember1,
    eMember2
}
t_myEnum bla = eMember1;

现在,我问反对 bla ++ 产生 eMember2 的原因是什么.

Now I ask what is the reason against bla++ yields eMember2.

用户定义的整数值赋值,例如

User defined assignments to integers values like

enum myenum {
    eMember1 = 0,
    eMember2 = 10
}

我认为这不会成为障碍.

shouldn't be an obstacle in my opinion.

推荐答案

我确信这是C和C ++的缺点.

I am sure it is a drawback of C and C++.

最初,枚举被视为整数常量集,作为指令 #define 的替代.因此,C中的枚举是此类常量集的通用名称.它们被制作得尽可能简单.:)

Initially enumerations are considered as sets of integer constants as an alternative for the directive #define. Thus an enumeration in C is a common name for a set of such constants. They were made as simple as possible.:)

C ++向前迈进了一步,枚举器开始具有其枚举类型.您也可以重载运算符++进行枚举,尽管我同意您的观点,内置这些运算符会更好.

There was made a step ahead in C++ and enumerators started to have types of their enumerations. Also you can overload operators ++ for enumerations though I agree with you that it would be better that these operators would be built-in.

例如,枚举可以与C ++中的std :: initializer_list类似的方式实现.

For example enumerations could be implemented a similar way as std::initializer_list in C++.

因此,我认为没有这些运算符是仅有历史原因.

So in my opinion there are only historical reasons for the absence of these operators.

另一个缺点是无法获得枚举中定义的枚举数.

An other drawback is impossibility to get the number of enumerators defined in an enumeration.

这篇关于为什么C中没有枚举的增量运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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