???????????reference?????pointer???????????????????????????????reference??????????pointer??
??????C++ ?У?reference???????????(pointer)??????????????????????C++??????????????reference??????pointer ???????Щ???????????????Щ???????????????????????????????reference???????????????? ???б???????reference??pointer?????????
?????????
??????pointer ????????reference?????????(object)????????????????????????????reference????????pointer???????????????????????????????????pointer???????????????? * ?? ??????reference???????????????? & ?? ???磬??????:
????int i = 3;
????????:
????int *pi = &i;
???????? pi ????????????????????????”???int????????”??????????????i??????????????棺
????int &ri = i;
???????? ri????reference??????????????????????????reference??????????i?? ??????????pointer??reference?????????????????????????????????????????????????????????????????????????????????? ???????????????????????
????Pointer ??reference???????pointer?????????????????? * ?????reference ????????dereference??????????????????????????????????“??ο?”?????reference??????κβ?????????ο??? ???磬 ?????????????е???壬 ?????? *pi ?? pi ??ο?????i?????? ????ri-??????κβ?????-?????ri??ο?????i?????? ??????p????????????????:
????*p = 4;
??????i??????4?? ?????reference ri?????????????д??
????ri = 4;
?????????????i??????4 ??
??????????????????????????????????????????????????pointer????reference?????????????????????????????????????????
??????????????????о??????enumeration????++????????????????????????C++?У? ?????++?????????о???????Ч?????磬 ?????漲?壺

 

enum day{
Sunday?? Monday?? …
};
day x;

???????? ++x ??????????????????????????????????????operator++???????????day?????????????? ++x ??????x????????? ????????????? operator++ ?? ??????day??????? ????:
????day operator++(day d);
???????????????????Ч???? ??????????????????(pass by value)??????ζ???????????????????????????????????????????????????????????????(operand)??????????????????reference???????????????
???????????????(passing by pointer)??????????????:
????day *operator++(day *d);
???????????????????洢??*d??????????????????(day)???????????????????????????++&x?????????????????????????????????????
?????????????????operator++??reference??????????????:

 

day &operator++(day &d)
{
d = (day)(d + 1);
return d;
}

???????????????? ???? ++x ?????????????????????????

$news$

????Passing by reference????????дoperator++??????????????????? C++????????и????????????? ???????????:
????day *operator++(day *d);
????????? ????????????????????????????????????????????? ???????????T?? T & ?? T const & ??????????????T???????(class)???о?(enumeration)????? ?????????????????????????????о?????????????????????????????????о????????????????????á?C++ ????????????????????????????ò?????????壬???????????????????????????壺
????int operator++(int i); // ????
??????????????int???????????? ++ ????塣 ?????????????壺
????int *operator++(int *i); // ????
?????????????? int * ???????????? ++ ????塣
????References vs. const pointers
????C++ ?в????????”const reference”?? ??????reference??????const????????????????reference??????????????????????°???????????????????? ?????reference??????д????????????°?????????????????磺
????int &ri = i;
?????? ri ???? i ????????????:
????ri = j;
??????????? ri ???? j ??????? j ?е?????? ri ????????????? i ??
???????????????pointer?????????????????????????????????reference????????????????Щ?????????? reference?? pointer??????????????ɡ????????reference??pointer???????? ????????reference??const pointer?????????????飬??????reference??????????????????????????????????????????????reference????? ??? ???reference????????????????????????reference?????????????κζ????????????????
????????ε????????????????????????const pointer??????????????????????????const pointer???? ???????????????? “pointers to const”??) ???磬???reference????????????????????????????????????

 

void f()
{
int &r = i;
}
??????????????????????????????
void f()
{
int &r; //????
}
????????????????????????????????????????????????
void f()
{
int *const p = &i;
}
???????????????????????
void f(){
int *const p; // ????
}

????????????? ???????reference????????reference??pointer??????????????????????????????????????
????Null references
??????????????????????????reference????????????????????????Ч??reference???????????????????????????????????????????????? ???????п???? ????????????κζ?????
????????????????????????????????????????????????????reference???????????? ???磬????????(swap function)????????????int?????????????????????????????????????

 

int i?? j;
swap(i?? j);
??????? i ?е????? j ?У? ????????? j ?е????? i ?С????????????д?????????
void swap(int *v1?? int *v2)
{
int temp = *v1;
*v1 = *v2;
*v2 = temp;
}

????????????£??????????????????: swap(&i?? &j);
????????????????????????????????п??????(null)????????????????????磬????
????swap(&i?? NULL);?????????????????
????????????????????reference???????

 

void swap(int &v1?? int &v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}

??????????????????swap????????????????????????????? ???????????????????????????????????????????????????Щ&?????????????????
????swap(i?? j);
??????????
??????Щ????????reference??????????????????????????? ?????reference?????????????????????????????Ч??reference??????????????Ч??????????????????????3????п? ???????Ч??reference???????????reference?? ???磬???????????reference???????????????????????????????
????int *p;
????…
????int &r = *p;
??????????*p??reference?????????????????reference???? ??????????????????????????reference???????????????????????????????ο??? ????????????ο?????????????????????????ζ??????????????????????????????????????п????????reference r ????*p (p?????????)?????p???????б???ο???????????????p????????????r????????????????????????????????????????и???????? ???????????????????Σ????
????????????????????????????Чreference???????

 

int &f()
{
int i;
return i;
}

?????????????????????????? i ??reference????????????????????????? i ??洢????????????????????????????????????????????reference???????????????????????????????????????Щ???? ?????????????????????????????п?????????
?????????reference????к?????????????????pointer????????????????reference???????????????????????????????????