Sonow I needtogoandaddonimplementationforthisvariable.
Tellyouwhat.
Wemayaswelljustdothathere.
Okay, nowit's justimplementation.
Let's runit.
Oh, well, atleastnowit's behavingin a somewhatexpectedway, butthisseems a littlebitof a mess.
I don't likehavingtohavethisimplementationsomewhereinmycode.
I wantedsomewheretiedtowherehavedeclaredit, because I couldeffectivelyputthisanywhere, and I mightputitinthewrongplace.
Well, modernseahas a solutionforthat.
Wecannowdeclarethisasbeinginlineonset.
Itsvaluecompilesjustfine.
Auntbehavesasexpected, Thisinlinekeywordhassortofmutatedoverthelast 20 yearsorso, Butonewaytothinkaboutitnowisthatwhenitsees a symbollikethisispartof a compileunit.
Thenit's thesameacrossalloftheCompile a bleunits, sothat's madeimplementingglobalvariables a littleeasier.
Onthewhole, the c++ languageallowsustodothatverywellbecauseithasscopevariable, soanyvariablesinside a functionsomewherecanonlyreallybechangedbythatfunction.
Thefactthat a functioncanchange a globalvariableandthereforealterthestateoftheentiresystemwithoutanywarningorhintthatithasdonethattotheusermeansthatwegetunpredictablebehaviorfromcallinthatfunctionsojustbeawareofthesethingswhenusingglobalvariablesforsimpleapplicationswhereyou'reinfullcontrol.
Sometimestheyofferquiteinterestingutility, sohewas a macrothattakesin a parameter s andthendeclares a variablecalled s onassignedittothestringvalueoff s.
Itturnsoutmacroshavequite a bitofsyntaxoftheirown.
Itisreallythecasethatweeverwanttoremovedthetypeofanobjectin a program.
Solet's lookatanalternativemethod, whichallowsimilarflexibilitybutdoesthingsin a slightlymoremodernwaygoingtoincludetheanylibraryfromthestandardlibraryandrewritemyfunctiontosupporttheanytypecomingin.
Soanyiseffectively a modern c++.
Wrapitforvoid, star, andwhereisVoid?
Startiscompletelytypelis.
Atleastherewe'vegot a typeofsomedescription.
Itis a standardanysoimmediatelythatsignifiestotheprogrammer.
Hey, thiscouldbeanythingatall.
Weuseitinmuchthesamewayasavoidstar, butitdoescomewith a KNaddedfeature.
Let's say I wantedtobringbacktheoriginalobjects.
I passeditin, asinanybut I wanttoconvertitbacktotypetest.
Well, I coulddothatwithstandardanycaste, andthat's verynice.
Butlet's say I wantedtocastmyanyvariableintosomethingthatdoesn't makesensesohere.
I'm tryingtocastitto a doublelikewedidbefore, So I'lljustchangemymainprogramheretocallthenewfunction.
Andyou'llnotice I'm nolongerpassingtheaddressbecause I'm passingin a referenceonWe'llrunitandseewhathappens.
Andit's highlightedthelineherethathey, look, itmadenosensetoconvertthisanyinto a doublebecausetheoriginaltypewasoftypetest.
Sothat's givenis a degreeofprotectionfromstoppingusdoingunnecessarilydodgyandfoolishthingswiththisgenericgeneralpurposevariableQuite a powerfulsafetyfeatureonbecauseitwasanexception, wecouldpotentiallywrapupthisinstructioninsideanexceptionhandlerandcatchitatruntimewithoutterminatingourprogram.
Well, I mustsay I'm impressed.
Welldone.
You'vemadeitthisfar.
Notmanypeoplehave a stomachlikeyou.
Let's seewhat's next.
Ohdear.
Usingnamespace S t B.
Thoseofyouthathavebeenprogramminglongenoughwillrecallthatweusedtocall a functionlikethis, thoughnowadaysyoumayhavenoticedthatcalling a functionis a littlebitmoreinvolved, andthisishappensduetotheintroductiononacceptance.
I wantyoutoimaginethatthissomeobjectstruckisactually a reallycomplicatedstructurewithallsortsofcomplicatedrulesregardingcopyingandmultipleinstances, etcetera, etcetera.
I'm justkeepingitverysimple.
Andinsuchcasesitmightnotalwaysbecorrectorconvenienttostoretheobjectin a container.
Instead, we'd ratherstore a pointertotheobjectinthecontainer, andthenwemightbetemptedtodosomethinglikethis.
Sowejustpushbackeightnewobject.
We'vecreateditandwepushthepointerbackin.
Sofar, sogood.
Then, atsomepointinyourprogram, youmaydecideWell, I'm donewiththoseobjectsnow, so I'llclearthatvector.
Andjusttomakethings a littlemorecomplicated, youmightdecidetodothat 100,000 timesinyourprogram.
Ifyou'recomingfrom a backgroundwhereyou'veworkedwithmoremodernlanguagesthatautomaticallyhandlethememoryforyou, youmightbethinking, Well, thatseemsquite a reasonableassumptiontomakebecausequite a fewofthelanguagesgotosomeeffortstohidememoryfromyou.
Admittedly, thisis a trivialexampleofthisproblem, anditwouldberemissofmenottointroducethemoremodern c++ wayofdoingthingsasprogrammingingeneralistrendingtowardsmodelsoffownershipandtheideathatobjectshavelifetimes.
C++ providesutilitiestohelpushandlethis, andwecouldmake a simplemodificationtothiscode.
Bye.
Insteadofusing a roar, Pointerwilluse a smartpointer.
Passitthetypeofsomeobjectwedon't needtospecify.
It's a pointtoexplicitlyanymore.
Andinsteadofspecifyingnewwillcallthemakeuniquefunction I knowdon't needtoworryaboutcleaningupaftermyself.
Infact, I canrelyontheclearmethodofthevectortoehandlethisformebecausetheuniquepointerortypicallysmartpointis, ingeneral a sensitivetowhenthememoryisnolongerrequiredonwillautomaticallycalledelite.