Tải bản đầy đủ - 0 (trang)
Chapter 31.  Don't write code that depends on the order of evaluation of function arguments

Chapter 31.  Don't write code that depends on the order of evaluation of function arguments

Tải bản đầy đủ - 0trang

Summary

Keep(evaluation)order:Theorderinwhichargumentsofa

functionareevaluatedisunspecified,sodon'trelyonaspecific

ordering.



Discussion

IntheearlydaysofC,processorregisterswereaprecious

resource,andcompilerswerehardpressedtoallocatethem

efficientlyforcomplexexpressionsinhigh-levellanguages.To

allowgenerationoffastercode,thecreatorsofCgavethe

registerallocatoranextradegreeoffreedom:Whencallinga

function,theorderofevaluationofitsargumentswasleft

unspecified.Thatmotivationisarguablylessstrongwithtoday's

processors,butthefactremainsthattheorderofevaluationis

unspecifiedinC++and,asitturnsout,varieswidelyacross

compilers.(SeealsoItem30.)

Thiscancausebigtroubletotheunwary.Considerthiscode:

voidTransmogrify(int,int);



intcount=5;

Transmogrify(++count,++count);//orderofevaluation



Allwecansayforcertainisthatcountwillbe7assoonas

TRansmogrify'sbodyisenteredbutwecan'tsaywhichofits

argumentsis6andwhichis7.Thisuncertaintyappliestomuch

lessobviouscases,suchasfunctionsthatmodifytheir

argument(orsomeglobalstate)asasideeffect:

intBump(int&x){return++x;}

Transmogrify(Bump(count),Bump(count));//stillunknown



PerItem10,avoidglobalandsharedvariablesinthefirstplace.

Butevenifyouavoidthem,others'codemightnot.For

example,somestandardfunctionsdohavesideeffects(e.g.,



strtok,andthevariousoverloadsofoperator<
ostream).

Thecureissimpleusenamedobjectstoenforceorderof

evaluation.(SeeItem13.)

intbumped=++count;

Transmogrify(bumped,++count);//ok



References

[Alexandrescu00c][Cline99]Đ31.03-05[Dewhurst03]Đ1415[Meyers96]Đ9-10[Stroustrup00]Đ6.2.2,Đ14.4.1

[Sutter00]Đ16[Sutter02]Đ20-21



ClassDesignandInheritance

Themostimportantsingleaspectofsoftware

developmentistobeclearaboutwhatyouaretrying

tobuild.

BjarneStroustrup

Whatkindsofclassesdoesyourteamprefertodesignand

build?Why?

Interestingly,mostoftheItemsinthissectionare

motivatedprimarilyorexclusivelybydependency

management.Forexample,inheritanceisthesecondstrongestrelationshipyoucanexpressinC++,second

onlytofriend;itshouldcomeasnosurprise,then,that

it'simportanttousesuchapowerfultooljudiciously,

correctly,andwell.

Inthissection,wefocusonthekeyissuesinclassdesign,

fromminimalismtoabstraction,fromcompositionto

inheritance,fromvirtualtononvirtual,frompublicto

private,fromnewtodelete:Howtogetthemright,how

nottogetthemwrong,howtoavoidthesubtlepitfalls,

andespeciallyhowtomanagedependencies.

Inthesectionafterthisone,we'llnarrowourfocus

specificallytotheBigFourspecialmemberfunctions:

Defaultconstruction,copyconstruction,copyassignment,

anddestruction.

OurvoteforthemostvaluableIteminthissectiongoesto

Item33:Preferminimalclassestomonolithicclasses.



32.Beclearwhatkindofclassyou're

writing

Summary

Discussion

References



Summary

Knowthyself:Therearedifferentkindsofclasses.Knowwhich

kindyouarewriting.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 31.  Don't write code that depends on the order of evaluation of function arguments

Tải bản đầy đủ ngay(0 tr)

×