??????????????C???п?????????????????c++?г????&????????????????????C???????????????????????????????C++??????????????????????C++??&?????????????????Ч???????????????????????????? c++??????(3)---?????????????????????????????????&???÷???????????????????????
??????????C++??????????????????C++?????????????????????????????????????????????????Ч??????????з?????????????????? ??????????Щ??????????????????????????????????и??屾???????????????????y?????????????????????????????????????? ????????á?
??????????ü??
??????????????????????????????????????????????????????????????
??????????????????????????? &??????=??????????
????????1????int a; int &ra=a; //????????ra?????????a?????????????
?????????
??????1??&?????????????????????????á?
??????2??????????????????????????
??????3?????????????????????????г??????
??????4?????????????????????????????????????????????????????????????????????????????????????????????
????ra=1; ????? a=1;
??????5???????????????????????????????????????????????????????????????????????????????????????????????????????洢???????????????÷???洢???????????????????????????????????&ra??&a????
??????6?????????????????á????????????????????????????????????????????????????????
???????????????
????1?????????????
????????????????????????????????????????C?????к???????????????????????д????????????????????????????????????????? ?????????????????????????????????????Ч????????????C++?У?????????????????Ч?????????Щ?????????????????????????? ?á?
????????2????
????void swap(int &p1?? int &p2) //??????????β?p1?? p2????????
????{ int p; p=p1; p1=p2; p2=p; }
???????????е???ú?????????????????????????????????????????ν??е??ü?????????????α??????κε?????????磺??????漲???swap?????????????????????д???
????main( )
????{
????int a??b;
????cin>>a>>b; //????a??b?????????
????swap(a??b); //????????a??b?????ε???swap????
????cout<<a<< ' ' <<b; //??????
????}
??????????????????????????????10 20????????????????20 10??
?????ɡ???2?????????
??????1???????????????????????Ч??????????????????????????βγ??????????????е???α????????????????????????????????????ж??βα???????????????????????????????????У????????
??????2????????????????????????????в???в?????ε????????????????β???????????????????????????????????????????????????? ?βη???洢??????βα???????α??????????????????????????????????????????????????????????????????????????????????????????Ч ??????????á?
??????3?????????????????????????????????????Ч???????????????????????????βη???洢????????????????"*????????"?? ????????????????????????????????????????????棬????????????????????????????????????Ρ????????????????????????
?????????????????????????Ч????????????????????????????????б???????ó????á?
????2????????
???????????????????const ???????? &??????=??????????
??????????????????????????????????????????????????????????????????const????????????????
????????3????
????int a ;
????const int &ra=a;
????ra=1; //????
????a=1; //???
????????????????????????Щ??????????????
????????4?????????????o?????????
????string foo( );
????void bar(string & s);
????????????????????????
????bar(foo( ));
????bar("hello world");
???????????foo( )??"hello world"???????????????????????C++?У???Щ?????????const???????????????????????????const??????????????const??????????????
??????????????????????????const??????£??????????const ??
????3??????????????
??????????÷?????????????????????????????
???????????? &?????????β??б???????????
????{??????}
?????????
??????1???????÷????????????庯????????????????&
??????2???????÷?????????????????????????в???????????????????
????????5?????3????ж????????????????fn1?????÷????????????????????????????????fn2????????????????????????
#include <iostream.h>
float temp; //??????????temp
float fn1(float r); //????????fn1
float &fn2(float r); //????????fn2
float fn1(float r) //???庯??fn1???????????????????????
{
temp=(float)(r*r*3.14);
return temp;
}
float &fn2(float r) //???庯??fn2?????????÷??????????
{
temp=(float)(r*r*3.14);
return temp;
}
void main() //??????
{
float a=fn1(10.0); //??1?????????????????????????????????????
float &b=fn1(10.0); //??2??????????????????? C++???в???漲??
//?????????????з???????????????????????????
float c=fn2(10.0); //??3????????????????????????
//?????????????з??????????????????
float &d=fn2(10.0); //??4????????????????????????
//?????????????з??????????????????
cout<<a<<c<<d;
}