本文介绍了为什么没有/限制检查枚举值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 考虑 enum Side {Back,Front,Top,Bottom}; enum side a; 现在,为什么 a = 124; 合法(好吧它真的是一个整数,但仍然, 检查可以执行..)? 如果 更多的类型检查会不会更有用? 解决方案 "星云" <无**** @ example.com>在留言新闻中写道:41 ****** @ news.wineasy.se ... 考虑枚举边{Back,Front,Top,Bottom}; enum side a; 现在,为什么 a = 124; 合法(好吧它真的是一个整数,但仍然可以进行检查......)?如果有更多的类型检查,那么枚举会不会更有用吗? 枚举类型是int的符号名称。声明 enum边a; 只是声明''a''为int。 Nebula< no **** @ example.com>写道: 考虑枚举边{Back,Front,Top,Bottom}; enum Side a; 现在,为什么 a = 124; 合法(它确实是一个整数,但仍然可以执行检查......)?如果有一个 好​​问题。它永远不是非法的,AFAIK;根据这两个标准,它肯定合法 。最初的原因可能是它不是 总是可以在编译时进行检查,导致运行时间 检查C中总是有的认为(并非没有理由)因为b $ b太昂贵了。有一种解决方法是通过制作枚举_not_ 真的是一个整数,但这可能会使一些现有的程序无效,这是标准委员会的事情。 总是(可以说是太)谨慎行事。 Richard Nebula写道: 考虑枚举边{Back,Front,Top,Bottom}; enum Side a; 现在,为什么 a = 124; 如果有更多的类型检查,那么就不会有更有用的功能? enum既不是一个集合也不是列表。 枚举工具是一个{方便}的方法来 将数字与符号相关联。没有更多 没有更少。 工厂不强制要求任何关系 amoung标识符。没有要求 每个标识符在枚举中具有唯一值。 语法允许关联 a枚举的名称(标识符)。 以下枚举说明了这一点: enum废话 {Moon = 5,Sun = 73,West = 4,Cake = 41623}; enum是的{one,two,frog,skip,my,lou}; / *请注意,符号一将* / / *与零值相关联。 * / 枚举不应被视为一种类型。 如果你想让枚举单独使用 类型,例如Pascal中的RECORD,然后你必须创建一个。 许多编程商店禁止使用enum工具。 他们更喜欢使用#define。 许多错误都是通过在枚举应该是什么(设置,列表等)上设置假设 来做出的。 ) 而不是它实际上是什么。 - 托马斯马修斯 C ++新闻组欢迎信息: http:// www。 slack.net/~shiva/welcome.txt C ++常见问题: http://www.parashift.com/c++-faq-lite C常见问题: http://www.eskimo.com/~scs/c-faq/top.html alt.comp.lang.learn.c-c ++ faq: http://www.comeaucomputing.com/learn/faq/ 其他网站: http://www.josuttis.com - C ++ STL图书馆书籍 http://www.sgi.com/tech/stl - 标准模板库 Considerenum Side {Back,Front,Top,Bottom};enum Side a;Now, why isa = 124;legal (well it really is an integer, but still,checking could be performed..) ?Wouldn''t enums be more useful if there was abit more typechecking ? 解决方案 "Nebula" <no****@example.com> wrote in message news:41******@news.wineasy.se... Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn''t enums be more useful if there was a bit more typechecking ?An enum type is a symbolic name for an int. Declaringenum Side a;is simply declaring ''a'' to be an int.Nebula <no****@example.com> wrote: Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn''t enums be more useful if there was a bit more typechecking ?Good question. It never has been illegal, AFAIK; it''s certainly legalunder both Standards. The original reason probably is that it''s notalways possible to do this checking at compile-time, leading to run-timechecks which have in C always been regarded (not without reason) asbeing too costly. There''s a way around that by making enums _not_ bereally an integer under the hood, but that would probably invalidatesome existing programs, which is something the Standard Committee hasalways been (arguably too) wary of doing.RichardNebula wrote: Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn''t enums be more useful if there was a bit more typechecking ?The enum is neither a set nor a list.The enum facility is a {convenient} method toassociate numbers with symbols. Nothing morenothing less.The facility does not mandate any relationshipamoung the identifiers. There is no requirementthat each identifier have a unique value withinthe enum.The syntax allows one to associatea name (identifier) to an enum.The following enums illustrates the point:enum Nonsense{Moon = 5, Sun = 73, West = 4, Cake = 41623};enum Yep {one, two, frog, skip, my, lou};/* Note that the symbol ''one'' will associate *//* with the value of zero. */An enum should not be considered as a type.If you want the enum to be have as a separatetype, such as the RECORD in Pascal, then youmust create one.Many programming shops ban the enum facility.They prefer to use #define instead.Many mistakes are made by placing assumptionson what an enum should be (set, list, etc.)rather than what it actually is.--Thomas MatthewsC++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txtC++ Faq: http://www.parashift.com/c++-faq-liteC Faq: http://www.eskimo.com/~scs/c-faq/top.htmlalt.comp.lang.learn.c-c++ faq: http://www.comeaucomputing.com/learn/faq/Other sites: http://www.josuttis.com -- C++ STL Library book http://www.sgi.com/tech/stl -- Standard Template Library 这篇关于为什么没有/限制检查枚举值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 06:01