helloandwelcometosomething a littlebitdifferentfrom a I'm goingtobetalkingabout a specificfeatureofthe C pluspluslanguagepointerson.
I felttherewas a needtodothisvideobecauseUNdiscordandonYouTubeandverymale, I'vebeenaskedquite a numberofquestionsaboutpeople's codethatdoesn't workondhe.
Invariably, theproblemseemstoliewith a misunderstandingofhowtousepointersinthefirstplace.
Sothisis a littlevideo I canusetoexcusethepunpointpeopleintherightdirectioniftheydohaveanyproblemswithpointersinmyvideooutputtodate, I'veneverreallytargeted a specificaudience, and I'm tryingnottowiththisvideo, either.
Soitdoesn't reallymatterifyou're a beginnerorintermediate.
Maybethere'llbesomethinginthisvideothatwillbenoveltoyou, andevenforthenon c++ programisouttherethatstillreallyrecommendwatchingit.
Becausepointis a fundamentalregardlessofwhichlanguageyou'reusing.
Itiswhereitis, butthevaluebeholdsisgoingtobe a memoryaddresssowecoulddeclareitexplicitly.
B equals 100 forexample.
Sothevaluegoesto 100.
What I'm tryingtodemonstratehereisthere's absolutelynodifferencebetweenthetwothings.
A pointerisjust a variablethatholdsanaddressinmemory.
It's veryrarethatwillactuallysetthevalueof a pointerexplicitlyandnumericallylikethis.
Instead, it's moreusefultousethelocationofanexistingvariableintheprogramasthevaluethatwegivetothepointeronDSIin C++ providesomethingwhichistheampersandsymbolwhichbehaveslikegettheaddressoff a variable.
Soherewe'vesaidourpointerbeWewanttosetitsvaluetobetheaddressofwherever a stored.
Sointhiscasewecansee a storethataddress 102 Sothevaluestoredinpoint B isoneoathtwo.
Nowthatwehave a locationinmemorythatstores a value, whichrepresents a memoryaddressto a differentlocation.
See, somewhereinourmemory, forwhatwe'resayingis, pleasegetthevalueatthepointedtolocationofourpointof B.
Sowe'lllookatthevalueof B, whichis 102 inthisinstance, andwe'llusethevalueof B asanaddressofftheactualvaluewe'reinterestedin.
Sothis 102 pointsto a effectively, whichiswhatwe'vesetuphereon, we'llgetthevalue.
Five.
Wehaveindirectlysetthevalueof C to a using a pointer, beandthatreallyisallthereistopointers, as I'vestated, and I willstatemanytimesinthisvideothatjust a variablethatholds a memoryaddress.
Butwe'vestartedtousethemultiplyasstrickstorepresentGetthevalueoff, and I thinkit's thisusageofadditionalsymbolsthatmakespointisquiteconfusingtothosethathaven't seenthembefore.
Butclearlyweneedtoincludesomethingelsebecausethisisnowsayingthatbeisjust a typeinterject, nodifference A and C wellin c n C plusplustosaythatthisis a pointertoaninterview.
WeincludetheasstricksimpleagainSowecanread.
Thisissaying, Pleasecreate a variablethatholds a memoryaddresstoanothervariablethatisaninterviewon.
Sometimesyou'llseeitwrittenoutlikethis, andsometimesyou'llseeitwrittenoutlikethis, bothofvalidButTut, tut, tut C and C pluspluswereusingthesamesymbolagaintorepresentsomethingelse.
Soeightbitsweknowthatin a C and C plusplusprogram, moreorlessallthetimeandinitisgoingtobe a 32 bitvalue, so I compilewillgoon, allocatesomememoryforVariable A, butit'lltakeupfourlocations.
That's justtoknowJesus a zero a one a twoon a threeIf I create a pointeragain, sointstar b equalstheaddressoff a onthistime, let's assumethatBayhasbeencreatedsomewhereelseinmemory.
It's giventhevalueof 100.
If I weretoincrementthepointer, so B equals B plusone.
Let's saywedidn't actuallyinitializeitas a string.
Wejustinitialized 10 bitesofcharmandnowwerunit.
Youknow, thingsarejustgettingworsehere.
What's goingon?
Andthat's becausewhenyouseeCharlesonpointersofCharles, it's verycommonthatyou'reactuallyinferring a string, andsothere's quite a lotofoperators, amongstmanyfunctionsacross C and C plusplusthatexpecttoseepointersoftypecharrepresentingstrings.
Andsotheydostringspecificthings.
Sointhiscase, thischarstarlocationthreeisbeingtreatedjustlike a regularstring.
A and I had a verysimilarsubobjectbeso a baseclassbasedobjectandtosubclassesofthatclasscalledsubobjectandsubject B inStan.
Sheatinganyoftheseobjectsisverysimple.
There's thebase.
Objectswillcreate a variablecalledobbase.
Likewise, forthetwosubjectsonhere I callidentifyyourselfonallthreeobjects.
Let's take a lookperfect.
Eachobjecthasidentifieditself.
Let's suppose I wantanarrayoffobjectson.
I wanttoreiteratethroughthatarrayonebyoneandgetthemtoidentifythemselveswell.
I cancreateanarrayof 10 basedobjectsandcreate a looptheloopsthroughall 10 andcallstheidentifyyourselffunction.
Andif I quicklyrunthatwell, see, weget 10 basedobjects.
Verygood.
Buthowdo I setoneoftheobjectstobeoneofthesubjectsofLet's pickObjectthreehereand I wanttoforcethatonetobeobject.
A Noobjectdayis a subclassoffthebaseobjectsthatthat's legitimate.
That's whythecompilerisnotcomplaining.
ButthewrongidentifyyourselfmethodiscalledMyBelt a fixedthatbyaddingin a virtualkeyword, whichtellsthecompilerthatif I amsubclassedon, anyofmyChildrenhappentooverridethismethod, pleasecallthechild's methodinstead.
Itdoesn't matterwhetherit's a child, objectornot, thearrayonlyconsistsoffsomebaseobject.
Whatwewantreallyis a structurethatallowsustohave a mixtureofbasesubjects, object A's andobjectbeesallinthesamearrayonLetthecompilerintelligentlyworkitalloutastowhichmethodshouldbecalledonwhichobject.
Faith, forexample, having a bigifstatementor a switchblockorsomethingsimilarthroughtheuseofpointers, thecompileronexecutingsubsystemiscapableofjustsortingitalloutforusperfect.
Now I'm getting a bitlonginthetooth, and I'm a wordthatarraysaren't verypopular.
Whenweaskedthecompilertocreateanarrayforus, sayinto a 10 whatwe'resayingis, pleasegoandfind a blockofmemoryforusthat's contiguousandreturnthisstartingaddressoffthatlocation.
So, forexample, itfindsmaybethefirstavailableblockofmemorythatitcan, whichishere.
Factorsarequitedeceptivebecausetheylooklike a raiseso I couldhave a vector, thatstore's endsandthefirsttimewepushedsomethingbacktothevector, itstoresitinRam.
Andthisiswhyitisbadtohave a pointertotheelementoffVectorbecausethePointeroriginallypointedtotheeighthelementhere, andthat's storedwhateveraddresswasnecessarytorepresentthatelement.
What I'm goingtodoisfakesomescopeintheprogrambyusingsomecurlybrackets, andwe'regoingtohave a lookatthebehaviorof a sharedpointerasitgoesinandoutofscope, sowe'llcreate a knob.
Jektwwithintheboundsofscopeonehereonwillcreate a secondpointerthatpointstothatobjectinscopetoLet's usethe D buggertoseewhathappens.
I think I'vecoveredallofthemostbasicusesofpointersin C++ andwe'veevenhad a quicklookatsmartpointersthatthereisnocodeforyoutolookatonthegethopethistime.
Butifyoufoundthisvideouseful, pleasegiveme a bigthumbsup.
Couldwehave a chatonthediscord?
Have a thinkaboutsubscribingand I'llseeyounexttime.
Takecare.
helloandwelcometosomething a littlebitdifferentfrom a I'm goingtobetalkingabout a specificfeatureofthe C pluspluslanguagepointerson.