??????????????????????????????????????constkeyword?ú??????μ?嶯????????????????СС????????????????????????????ν????????????????£??????????????????÷???ū?????????????????????????η????????ò????????????????????????????
??????????????constkeyword????????????????峣?????????????д??const??constant????д???????????????÷?????????Щ??
???????????????????????
??????C?????У?const????÷???????ж??峣????????????????????κ??????????
????????C++?????С?????????????????????????κ?????????壬??????????????????????????????????????е???????????????峣??????÷????????????????????????κ???????????κ??????????????κ?????????塣
????0?????α????????
????int ii=0;
????const int i=0;           //i???????i??????????
????const int *p1i=&i;        //???p1i????????????????????????
????int const* p11i = &i; //????const??*??????????????????
????int  * const p2i=ⅈ    //???p2i?????????????????
????const int * const p3i=&i; //???p3i????????????????????
????p1i=ⅈ                 //???
????*p2i=100;               //???
????1?????κ??????
???????????????????????????????????????????????????????????const???Ρ?????const?????????????????????????????????????????????????????????????????????????
??????1???????????????????????????????????????????????????????????????????????豣??????????????const ???Ρ?
???????硣???????void Func1(int x)??д??void Func1(const int x)??????????С?
????????????void Func2(A a)??????д??void Func2(const A a)??????A????????????????????
??????2????????????????????const????????????????????????и???????????á?
???????硣???StringCopy???????????void StringCopy(char *strDest?? const char *strSrc)??????????????????????strSrc????????????????????
??????3?????????????????
?????????????????£????????????????????????????????????????????????????????????void Func(A a) ????????????????Ч???????????????????????A ??????????????????????a????????????????????????????????????????????Ч???????????????????void Func(A& a)?????????????????????????壬?????????????????????????????????????
???????????????????????????и???????a???????????????????????????????費????????????????????????????const???Ρ??????????????????????void Func(const A& a)????????????void Func(int x) ??д?void Func(const int &x)????????Ч???????б????????????????????????????????????????????????????“?????”??“???????”??Ч?????????
??????????const??????????????????÷?????
??????1??????????????????????????????y?“?????”???????“const ???????”??????????Ч???
???????罫void Func(A a) ???void Func(const A &a)??
??????2????????????????????????????????“?????”???????“const ???????”?????????????Ч??????????????????????????????void Func(int x) ???????void Func(const int &x)??
????2?????κ?????????
??????????????·????????????????????????????????????????
??????1?????軀??????????“?????”????????????????????????????????洢????У???const ??????в??????????
???????磬????????int GetInt(void) д??const int GetInt(void)??
??????????????A GetA(void) д??const A GetA(void)??????A ?????????????????????
??????2?????軀??????????“?????”????????????????????????????????????????÷?????????????????const ???ε?????????
???????磬???庯?????const char *GetString(void)?????char *str = GetString()????????????????д??const char *str = GetString()??
??????3?????軀????????????“???????”???????????????????????Ч???????????????ó?????????????????????????????????????????????“????”?????????“????”??????????????????
???????硣?????????????????A & operate = (const A &other)?????費??cons???Σ?????A a?? b?? c??(a = b) = c????????????????????const???Σ???const A & operate = (const A &other)???????????
????3?????κ?????????塣
????????const???????????????constkeyword?????????????β????????????????????????????????????????const ?????
???????????дconst ??????????????????????????????????????????const ?????????????????????????????????????????
???????硣????????У???stack ????????GetCount ???????????????????GetCount ????const ?????????????????GetCount ?????е????
class Stack
{
public:
void Push(int elem);
int Pop(void);
int GetCount(void) const; // const ???????
private:
int m_num;
int m_data[100];
};
int Stack::GetCount(void) const
{
++ m_num; // ???????????????????m_num
Pop(); // ?????????????÷?const ????
return m_num;
}
??????????constkeyword???????????????????????const????????
????1) const????????????const?????????????const???????????????????????????const?????????
????2) const????????????????????const??????????????????????????????
????3) const??????????????????????????????????????const????.?????????????????????????????????м?顣
????4) ???????mutable???η????????????????????????????????????ζ???????????????const????????????????????