枚举的两个值一样有什么意义?

believefym 2007-11-30 10:09:56
enum XX{a=0,b,c=1,d};
int main()
{
XX test = b;
if(test==b)
cout<<"y"<<endl;
if(test==c)
cout<<"y"<<endl;
return 0;
}

因为b和c都为1,输出为两个y,这样的设计有什么意义呢?感觉反而不好了,谁给解释一下
...全文
1363 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuhui_7810 2007-12-01
  • 打赏
  • 举报
回复
没有什么深层次的意义,只是有时须要中间两个为同一值而已
霁云 2007-12-01
  • 打赏
  • 举报
回复
枚举变量只是增加了程序的可读性而已,所指向内容为同值只是巧合
thecorr 2007-12-01
  • 打赏
  • 举报
回复
guanzhu
独孤过儿 2007-12-01
  • 打赏
  • 举报
回复
enum XX{a=0,b,c=1,d};

枚举中的XX可以等价的想象成int类型,然后a、b、c、d可以想象成一个const类型的变量,那这个问题就和下面的代码没有什么区别了:

const int a = 0;
const int b = 1;
const int c = 1;
const int d = 2;
visame 2007-12-01
  • 打赏
  • 举报
回复
基本同意tangshuiling
http://kolpackov.net/projects/c++/enum/
In The Design and Evolution of C++ Bjarne Stroustrup gives interesting details about the
history of enumerations in C and C++. Apparently, they were not part of
the original C design and were reluctantly added later. Bjarne also admits
that he was not very interested in enumerations as a feature and added
them with some minor modifications needed to fit into the concept of
user-defined types. It should not be a surprise, therefore, that enumeration
types abound in all sorts of little defects and inconveniences.
tangshuiling 2007-11-30
  • 打赏
  • 举报
回复
设枚举表示为一些人的体重 enum {zhangsan=70,wangwu=70,zhaoliu=80};
接下来便可以如此来用cout<<"zhangsan weight:"<<zhangsan<<"wangwu weight:"<<wangwu<<endl;
xalangying 2007-11-30
  • 打赏
  • 举报
回复
enum{Sunday=7, Week7 = 7};
为什么会没用就像 string 提供 length 也提供 size 一样
mu_yang 2007-11-30
  • 打赏
  • 举报
回复
7月31天
8月31天
但你不能说7月也是8月
ms_ms 2007-11-30
  • 打赏
  • 举报
回复
关注。。。

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧