Definition
intrate=80;int&pt=rate;
⒈ptisareferencevariable,itmustbeinitializedwhendeclaringthereferencevariable.ThevalueofptandratePointtothesamememoryvariable.Themostusefuluseofreferenceisasafunctionparameter(structureandobject).Byreferencingavariableasaparameter,thefunctioncanusetheoriginaldatainsteadofcopying.
⒉ThecharacteristicsofreferencearecloserForaconstpointer,onceitisassociatedwithavariable,itwillalwaysbeloyaltoit.Thereferencecanbesetbyinitialization,butitcannotbesetbyassignment
intother=30;pt=other;
Atthistime,thevalueofrateandthevalueofpthavebeenchangedto30,buttheaddressisstilltheaddressoftheoriginalrate.Nottheaddressofother.
⒊Iftheprogramdoesnotwanttochangetheinformationpassedtoit,butIfyouwanttouseareferenceagain,youshoulduseaconstantreference
inttest(constint&p)//Whenyouchangethevalueofp,thecompilerwillgenerateanerrormessage
⒋IftheactualparameterIfitdoesnotmatchtheapplicationparameters,C++willgeneratetemporaryvariablesforthispurpose.Thisisonlyallowedwhentheparametersareconstreferences.Inthefollowingcases,temporaryvariableswillbegenerated,andthefunctionparameterswillreferencethevariables.
****Whentheparameterisnotaconstreference,ifitisnotanlvalue(andcannotgenerateatemporaryvariable),itwillcauseacompilationerror*****(emphasizedthethirdpoint,thatis,trytouseconst)
Thetypeoftheactualparameteriscorrect,butitisnotanlvalue
.Thetypeoftheactualparameterisincorrect,butitcanbeconvertedtothecorrecttype
Theconceptoflvalue:exceptforliteralconstantsandexpressionscontainingpolynomialsThedataobjectthattheformulacanbereferenced
⒌Trytodeclarethereferencereturnedbythefunctionasconsttopreventyoufrommodifyingitsvaluethroughthereturnedreference.Thereturnreferenceisactuallythealiasofthereferencedvariable.
constsysop&use(sysop&pref)sysoppref=();sysoptest=use(pref);
equivalentto
user(pref);test=pref;
⒍Thecharacteristicofinheritanceisthatthereferenceofthebaseclasscanpointtothederivedclassobjectwithoutforcedtypeconversion.Thisadvantageisthatyoucandefineafunctionthatacceptsthebaseclassreferenceseatparameter.Classobjectasaparameter,youcanalsouseaderivedclassasaparameter
If
int&ir=0
meansthattheaddressis0,notareferenceMeaning,therearevariableassignmentsinthedeclarationInthecaseofvalue,thevariablemustbeusedasthereferencedobject,otherwisethesystemcannotrecognizewhetheryourstatementisareferenceoranassignment!
Instructionsforuse
Ifint&ir=0,itmeansthattheaddressis0,notareference.Ifthereisavariableassignmentinthedeclaration,thevariablemustbeusedasareferenceTheobject,otherwisethesystemcannotrecognizewhetheryourstatementisareferenceoranassignment!
⑴&;Hereitisnotforaddresscalculation,butforidentification.
⑵Thetypeidentifierreferstothetypeofthetargetvariable.
(3)Whendeclaringthereference,itmustbeinitializedatthesametime.
⑷Afterthequoteisdeclared,itisequivalenttothetargetvariablenamehastwonames,namelytheoriginalnameofthetargetandthequotedname,andthequotednamecannotbeusedasanaliasforothervariablenames.
ra=1;isequivalenttoa=1;
⑸Declaringareference,notanewdefinitionofavariable,itonlymeansthatthereferencenameisanaliasofthetargetvariablename,Itisnotadatatypebyitself,sothereferenceitselfdoesnotoccupythestorageunit,andthesystemdoesnotallocatestorageunittothereference.Therefore:toseektheaddressofthereferenceistoseektheaddressofthetargetvariable.&raisequalto&a.
⑹Itispossibletoestablishareferencetoanarray.InC++,thesolutiontothepricereductionproblemofanarrayasaformalparameteristousethereferenceofanarray
⑺Thereferencetothereferencecannotbeestablished,andthepointertothereferencecannotbeestablished..Becauseareferenceisnotadatatype,thereisnoreferencetoareference,noreferencetoapointer.
Forexample:
intn;
int&&r=n;//error,thecompilersystemregards"int&"asone,Regard"&r"asawhole,thatis,areferenceisestablished,andthereferencedobjectshouldbeavariableofacertaindatatype
int&*p=n;//error,compilesystemRegarding"int&"asawhole,and"*p"asawhole,thatis,apointertoareferenceisestablished.Thepointercanonlypointtoavariableofacertaindatatype.
⑻Itisworthmentioningthat,Youcancreatepointerreferences
Forexample:
int*p;
int*&q=p;//Correct,thecompilersystemputs"int*"Thinkofitasone,and"&q"asone,thatis,createareferencetothepointerp,thatis,givethepointerpanaliasq.
Principlesofuse
Forfunctionsthatarepassedbyvaluewithoutmodification:
.Thedataobjectissmall,anditispassedbyvalue
.Ifthedataobjectisanarray,defineaconstpointer
.Ifthedataobjectisalargerstructure,youcanuseaconstpointerorcosntreference,
.Ifitisaclassobject,thenUseconstreference.
Forfunctionsthatmodifythevalue
.Ifitisbuilt-indata,usepointers
.Arrays,youcanonlyusepointers
.Structureusingpointerorreference
.Object.Usereference