


下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C+基础(enum类型分析)enum在实际中应用比较少,容易被忽略。其实enum和struct、class一样,者B是用户自定义类型。既然是自定义类型,就可以有他的数据成员,还有成员函数!Forexample:enumea=1,b=2,c=4;那么:001:enumee1;/enume不是对象,它是类型,e1才是类型enum的对象!002:ee1;/e是类型enume的简写003:e1=1;/错误!考试大提示int不能赋值给一个用户自定义类型004:e1=e();/默认构造函数005:e1=e(1)/从int构造enume类型对象的构造函数006:e1=a;/默认调用"拷贝构造函数&
2、quot;enum的"取值范围"和"内存分配"如何确定一个enum的取值范围?Forexample:enume1a=2,b=4;首先找到其绝对值的值,但为了容易理解,先不谈负数,也就是先找到其值,这里的值是4。4用二进制表示就是100,也就是需要3bits才能最小的容纳下4这个值,而3bits所能表示的范围是0-7,于是e1的取值范围为0,7。现在来看看负数,enume2a=-2,b=4;其中绝对值的是4,需要3bits才能容纳下,但因为可以取负值(而元素b=4不是负值),也就是说需要增加一个符号位,那么就需要4bits。4bits的取值范围是1000-
3、0111(二进制表示),也就是-8到7(十进制表示)。enume3a=-4,b=2就只需要3bits,取值范围是-4,3。简单的说就是找到最少的能容纳下所有的元素的位数因为C+幅准规定超出枚举类型表示范围的赋值结果是undefined的,也就是说e2x=(e2)6是肯定正确的,而e2y=(e2)8行为是未定义的。enum的内存分配,如e2需要3bits,那么C+M定e2的尺寸只要容得下3bits就行,到底是取1个byte,还是4个byte,还是,那由编译器自己决定。但是,C+幅准在这里有个限制:1<=sizeof(enmu)<=sizeof(int)。以上内容参考自BjarneSt
4、roustrup的TheC+ProgrammingLanguane.原文如下:4.8Enumerationsdcl.enumAnenumerationisatypethatcanholdasetofvaluesspecifiedbytheuser.Oncedefined,anenumerationisusedverymuchlikeanintegertype.Namedintegerconstantscanbedefinedasmembersofanenumeration.Forexample,enumASM,AUTO,BREAK;definesthreeintegerconstants,ca
5、lledenumerators,andassignsvaluestothem.Bydefault,enumeratorvaluesareassignedincreasingfrom0,soASM=0,AUTO=1,andBREAK=2.Anenumerationcanbenamed.Forexample:enumkeywordASM,AUTO,BREAK;Eachenumerationisadistincttype.Thetypeofanenumeratorisitsenumeration.Forexample,AUTOisoftypekeyword.Declaringavariablekey
6、wordinsteadofplainintcangiveboththeuserandthecompilerahintastotheintendeduse.Forexample:voidf(keywordkey)switch(key)caseASM:/dosomethingbreak;caseBREAK:/dosomethingbreak;)Acompilercanissueawarningbecauseonlytwooutofthreekeywordvaluesarehandled.Anenumeratorcanbeinitializedbyaconstantexpression(§
7、C.5)ofintegraltype(§4.1.1).Therangeofanenumerationholdsalltheenumeration'senumeratorvaluesroundeduptothenearestlargerbinarypowerminus1.Therangegoesdownto0ifthesmallestenumeratorisnonnegativeandtothenearestlessernegativebinarypowerifthesmallestenumeratorisnegative.Thisdefinesthesmallestbitfi
8、eldcapableofholdingtheenumeratorvalues.Forexample:enume1dark,light;/range0:1enume2a=3,b=9;/range0:15enume3min=10,max=1000000;/range1048576:1048575Avalueofintegraltypemaybeexplicitlyconvertedtoanenumerationtype.Theresultofsuchaconversionisundefinedunlessthevalueiswithintherangeoftheenumeration.Forexa
9、mple:enumflagx=1,y=2,z=4,e=8;/range0:15flagf1=5;/typeerror:5isnotoftypeflagflagf2=flag(5);/ok:flag(5)isoftypeflagandwithintherangeofflagflagf3=flag(z|e力ok:flag(12)isoftypeflagandwithintherangeofflagflagf4=flag(99);/undefined:99isnotwithintherangeofflagThelastassignmentshowswhythereisnoimplicitconver
10、sionfromanintegertoanenumeration;mostintegervaluesdonothavearepresentationinaparticularenumeration.ThenotionofarangeofvaluesforanenumerationdiffersfromtheenumerationnotioninthePascalfamilyoflanguages.However,bitmanipulationexamplesthatrequirevaluesoutsidethesetofenumeratorstobewelldefinedhavealonghistoryinCandC+.Thesizeofanenumerationisthesizeofsomeintegraltypethatcanholditsrangeandnotlargerthansizeof(int),unlessanenumeratorcannotberepresentedasanintorasanunsignedint.Forexample,sizeof(e1)couldbe1ormaybe4butnot8onamachinewheresizeof(int)=4.Bydefault,enumerationsa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司重要涉密人员合同标准文本
- 2025年瑜伽教练资格证考试题库:初级理论模拟试题
- 2025年初中地理模拟考试卷地理国情认知地理信息工程案例分析试题
- 2025年小学语文毕业升学考试全真模拟卷(语文综合素养测评)-阅读理解能力提升策略篇
- 2025年机电工程管理与实务一建考试法规题库详解与实战题卷
- 2025年一建机电工程管理与实务考试机电工程技术前沿智能传感器试题卷
- 2025年专升本艺术概论考试模拟试题(艺术鉴赏提升与艺术素养)
- 2025农产品运输合同
- 2025年小学语文毕业升学模拟试卷(口语表达与沟通技巧专项试题)
- 2025年江苏省南京市秦淮区重点中学三模生物试题试卷含解析
- 中外航海文化知到课后答案智慧树章节测试答案2025年春中国人民解放军海军大连舰艇学院
- 2025年华润燃气投资中国有限公司招聘笔试参考题库含答案解析
- (正式版)YBT 6328-2024 冶金工业建构筑物安全运维技术规范
- 水利工程管理单位定岗标准(试点)
- 2022年高考全国甲卷:写作指导及范文课件16张
- 无菌技术操作PPT
- DBJT15-82-2021 蒸压加气混凝土砌块自承重墙体技术规程
- 2022年《跟徐老师学汉语》新HSK六级词汇词
- 妊娠剧吐诊断以及临床处理专家共识
- [PPT]桥梁工程桩基施工超全解析(41页 配图丰富)_ppt
- 叉车定期检验研究分析报告
评论
0/150
提交评论