{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS135 Notes.pdf - *Automatic​ ​storage​ ​is​...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: *Automatic​ ​storage​ ​is​ ​an​ ​example​ ​of​ ​the​ ​principle​ ​of​ ​least​ ​privilege,​ ​which​ ​is​ ​fundamental​ ​to good​ ​software​ ​engineering. In​ ​the​ ​context​ ​of​ ​an​ ​application​ ​(a​ ​program),​ ​the​ ​principle​ ​states​ ​that​ ​code​ ​should​ ​be​ ​granted only​ ​the​ ​amount​ ​of​ ​privilege​ ​and​ ​access​ ​that​ ​it​ ​needs​ ​to​ ​accomplish​ ​its​ ​designated​ ​task,​ ​but​ ​no more. *Point:​ ​An​ ​inline​ ​function​ ​can​ ​improve​ ​performance​ ​if​ ​it's​ ​small *Point:​ ​Defining​ ​a​ ​small​ ​member​ ​function​ ​inside​ ​the​ ​class​ ​declaration​ ​(.h)​ ​does​ ​NOT​ ​promote​ ​the best​ ​software​ ​engineering,​ ​because​ ​clients​ ​of​ ​the​ ​class​ ​will​ ​be​ ​able​ ​to​ ​see​ ​the​ ​implementation​ ​of the​ ​function,​ ​and​ ​the​ ​client​ ​code​ ​must​ ​be​ ​recompiled​ ​if​ ​the​ ​function​ ​definition​ ​changes.​ ​ ​If​ ​you have​ ​your​ ​implementation​ ​separate​ ​from​ ​your​ ​declaration​ ​and​ ​have​ ​designed​ ​it​ ​well​ ​you​ ​should be​ ​able​ ​to​ ​change​ ​your​ ​implementation​ ​without​ ​changing​ ​the​ ​declaration *Conclusion:​ ​Only​ ​the​ ​simplest​ ​and​ ​most​ ​stable​ ​member​ ​functions​ ​(i.e.,​ ​whose​ ​implementations are​ ​unlikely​ ​to​ ​change)​ ​should​ ​be​ ​defined​ ​in​ ​the​ ​class​ ​header. Basically​ ​what​ ​we​ ​are​ ​trying​ ​to​ ​do​ ​here​ ​is​ ​be​ ​able​ ​to​ ​determine​ ​when​ ​constructors​ ​and destructors​ ​are​ ​called​ ​and​ ​in​ ​what​ ​order First​ ​you​ ​have​ ​to​ ​classify​ ​the​ ​type​ ​of​ ​object ♦​ ​Global​ ​(or​ ​static​ ​global) ​ ​♦​ ​Static​ ​local ♦​ ​automatic​ ​(non-static​ ​local)​ ​ ​Constructor​ ​Order ​ ​♦​ ​Global​ ​instances ​ ​IF​ ​you​ ​have​ ​more​ ​than​ ​one​ ​global,​ ​in​ ​order​ ​of​ ​instantiation ♦​ ​All​ ​local​ ​instances​ ​(Regular​ ​and​ ​static)​ ​ ​When​ ​the​ ​object​ ​is​ ​instantiated,​ ​in​ ​order​ ​ ​Destructor Order ♦​ ​When​ ​a​ ​local​ ​function​ ​ends​ ​ ​ALL​ ​non-static​ ​local​ ​instances​ ​are​ ​destroyed​ ​in​ ​the​ ​reverse​ ​order they​ ​were​ ​created ​ ​♦​ ​When​ ​main​ ​ends ​ ​ALL​ ​non-static​ ​local​ ​instances​ ​in​ ​main​ ​are​ ​destroyed​ ​in​ ​the​ ​reverse​ ​order​ ​they​ ​were​ ​created ​ ​All​ ​static​ ​local​ ​instances​ ​(from​ ​all​ ​functions)​ ​are​ ​destroyed​ ​in​ ​the​ ​reverse​ ​order​ ​they​ ​were created​ ​ ​All​ ​global​ ​instances​ ​(from​ ​all​ ​functions)​ ​are​ ​destroyed​ ​in​ ​the​ ​reverse​ ​order​ ​they​ ​were created. Desctructor​ ​constructor​ ​codes Everything​ ​in​ ​C++​ ​is​ ​pass​ ​by​ ​value​ ​UNLESS​ ​specified​ ​otherwise *Pass​ ​by​ ​value​ ​has​ ​an​ ​advantage​ ​for​ ​a​ ​object​ ​if​ ​the​ ​function​ ​does​ ​not​ ​need​ ​to​ ​have​ ​access​ ​(be able​ ​to​ ​modify)​ ​to​ ​the​ ​members​ ​of​ ​the​ ​original​ ​object​ ​argument​ ​(remember​ ​the​ ​principle​ of​ ​least privilege) *When​ ​an​ ​argument​ ​is​ ​passed​ ​by​ ​constant​ ​reference​ ​this​ ​means​ ​that​ ​only​ ​a​ ​reference​ ​to​ ​the original​ ​variable​ ​is​ ​passed​ ​to​ ​the​ ​function​ ​BUT​ ​the​ ​argument​ ​cannot​ ​be​ ​changed​ ​by​ ​the​ ​function –​ ​so​ ​we​ ​minimize​ ​memory​ ​use​ ​and​ ​keep​ ​the​ ​principle​ ​of​ ​least​ ​privilege Include​ ​pass​ ​by​ ​const​ ​reference private​ ​utility​ ​function ...
View Full Document

{[ snackBarMessage ]}