Effective_C++学习笔记

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

E®ectiveC++(3rd)2008122C++11.101C++.........................11.202const,enum,inline#de¯ne.................11.303const............................21.404....................3//52.105C++..................52.206...................................52.307virtual....................62.408.........................72.509virtual...............72.610operator=referenceto*this................72.711operator=....................82.812.....................10113.116newdelete................11134.119classtype.........................134.220pass-by-reference-to-constpass-by-value...........13155.126..................155.228handles..................165.330inlining......................16iii196.133........................196.236non-virtual..............216.337.................226.438has-a.............236.540......................23257.142typename.......................257.244templates..................26C++1.101C++C++multiparadigmprogramminglanguageproceduralobject-orientedfunctionalgenericmetaprogrammingC++sublanguage²C²Object-OrientedC++classencapsulationinheritancepolymorphismvirtual......²TemplateC++C++genericprogramming²STLSTLtemplatecontainersiteratorsalgorithmsfunctionobjectstemplates1.202const,enum,inline#de¯neC++classstaticintegraltype,int,char,bool:classGamePlayerfprivate:staticconstintNumTurns=5;//intscores[NumTurns];//...g;12C++NumTurnsclassconstintGamePlayer::NumTurns;//NumTurnsstaticin-class:classCostEstimatefprivate:staticconstdoubleFudgeFactor;//staticclass...g;constdoubleCostEstimate::FudgeFactor=1.35;//staticclassclassclassGamePlayer::scoresstaticclassinclasstheenumhackenumeratedtypeintGamePlayerclassGamePlayerfprivate:enumfNumTurns=5g;intscores[NumTurns];...g;1.303constSTLT*constconstT*const:std::vectorintvec;...conststd::vectorint::iteratoriter=vec.begin();//iterT*const++iter;//iterconstSTLconstT*constiteratorstd::vectorint::constiteratorcIter=vec.begin();*cIter=10;//*cIterconst++cIter;//1.4043,,:classRationalf...g;constRationaloperator*(constRational&lhs,constRational&rhs);Rationala,b,c;...(a*b)=c;//operator*1.404CpartofC++01non-CpartsofC++arrayCpartofC++vectorSTLpartofC++C++memberinitializationlistconstreferencesC++baseclassesderivedclassesclassbaseclasses|non-localstaticP30-P33²C++²memberinitializationlistassignmentclass²localstaticnon-localstatic4C++//2.105C++defaultcopycopyassignmentclassEmptyfg;classEmptyfpublic:Empty()f...g//defaultEmpty(constEmpty&rhs)f...g//copy»Empty()f...g//Empty&operator=(constEmpty&rhs)f...g//copyassignmentg;publicinline30non-virtual07classbaseclassvirtualbaseclassescopyassignmentprivatederivedclassescopyassignment2.206publiccopyingcopycopyassignmentprivate²privateUncopyablebaseclassP3956//2.307virtualC++derivedclassbaseclassbaseclassnon-virtual|derivedclassvirtualvirtualclassvirtualbaseclassclassbaseclassvirtualpurevirtual:classAWOVfpublic:virtual»AWOV()=0;g;classpureclassclassvirtualpurevirtualAWOV::»AWOV()fg//purevirtualmostderivedclassbaseclassAWOVderivedclasses»AWOVbaseclassesvirtualpolymorphicbaseclassesbaseclassesbaseclassderivedclassbaseclassesstringSTLbaseclassesclassesbaseclasses²polymorphicbaseclassesvirtualclassvirtualvirtual²Classesbaseclassespolymorphicallyvirtual2.40872.408C++²²class2.509virtualBaseclassvirtualderivedclassesbasebaseclassesvirtualvirtualderivedclassbaseclassbaseclassderivedclass²virtualderivedclass2.610operator=referenceto*thisx=y=z=15;x=(y=(z=15));referenceclasses:8//classWidgetfpublic:...Widget&operator=(constWidget&rhs)//reference,f...return*this;//g...g;+=-=*=²assignmentreferenceto*this2.711operator=classbitmapclassBitmapf...g;classWidgetf...private:Bitmap*pb;//heapg;operator=Widget&Widget::operator=(constWidget&rhs)//operator=fdeletepb;//bitmappb=newBitmap(*rhs.pb);//rhsbitmapreturn*this;//10goperator=identitytest2.711OPERATOR=9Widget&Widget::operator=(constWidget&rhs)//operator=fif(this==&rhs)return*this;//identitytestdeletepb;pb=newBitmap(*rhs.pb);return*this;gnewBitmapWidgetBitmapoperator=pbpbWidget&Widget::operator=(constWidget&rhs)fBitmap*pOrig=pb;//pbpb=newBitmap(*rhs.pb);//pb*pbdeletepOrig;//pbreturn*this;goperator=copyandswapclassWidgetf...voidswap(Widget&rhs);//*thisrhs29...g;Widget&Widget::operator=(constWidget&rhs)fWidgettemp(rhs);//rhsswap(temp);//*thisreturn*this;g²operator=copy-and-swap²10//2.812OO-systemscopycopyassignmentcopyingderivedclasscopyingbaseclass:classCustomerf...g;classPriorityCustomer:publicCustomerfpublic:PriorityCustomer(constPriorityCustomer&rhs);PriorityCustomer&operator=(constPriorityCustomer&rhs);...private:intpriority;g;PriorityCustomer::PriorityCustomer(constPriorityCustomer&rhs):Custormer(rhs),priority(rhs.priority)fgPriorityCustomer&PriorityCustomer::operator=(constPriorityCustomer&rhs)fCustomer::operator=(rhs);priority=rhs.priority;return*this;gcopying1local2baseclassescopying²Copyingbaseclass²copyingcopyinginitcopying3.116newdeletenewnewoperatornew4951deletedeletedeletedeletedelete::std::string*stringPtr1=newstd::string;std::string*stringPtr2=newstd::string[100];...deletestringPtr1;//delete[]stringP

1 / 31
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功