????C++?е?const??????????????????????????????????????????壬??????????
????Const??????
????C++?е?const?????????????????????????????????????????????п???Υ??const???????????????????error??
???????????????????????????????????????bit??????????????????????????????????????C++?в??????????????????????????????
????struct A {
????int *ptr;
????};
????int k = 5?? r = 6;
????const A a = {&k};
????a.ptr = &r; // !error
????*a.ptr = 7; // no error
????a??const???????a???κγ?????и?????????error????????????ptr????????ptr???????????????????????????Υ?????????????????A??????????????
?????????????????????????const?????п??????Щ??????????????????????Υ????????????Effective C++?е????????
????class CTextBlock {
????public:
????...
????std::size_t length() const;
????private:
????char *pText;
????std::size_t textLength;            // last calculated length of textblock
????bool lengthIsValid;                // whether length is currently valid
????};
????CTextBlock??????ε???length????????????????????浽textLength????У???lengthIsValid???????????????Ч????????????textLength??lengthIsValid???????????????Υ??CTextBlock??????????????????????????????е??Щbit???????????????C++???????????????????mutable??????
?????????????C++??const?????????????????????????mutable???????????????????????????
????Const???α???
?????????????????const???α??????????????????????????ж????????????????????????const?????????????????????
????const int i;
????i = 5; // !error
????const int j = 10; // ok
??????????????????????????????????????????????????????????????????const???????????????????????????????????????????????????
????const int COMPILE_CONST = 10;
????const int RunTimeConst = cin.get();
????int a1[COMPLIE_CONST]; // ok in C++ and error in C
????int a2[RunTimeConst]; // !error in C++
???????C++??????????????鳤???г????????????????滻?????????????????????滻????gcc??????????????鳤??????????????滻??????????COMPILE_CONST???????????н????滻????
??????????const???????????????????????????b.cpp?????a.cpp?е?const????M???????M????????????extern??
????//a.cpp
????extern const int M = 20;
????//b.cpp
????extern const int M;
????????????????????????.h????л???????include??.h?????.cpp??????д????????壬????????????????????const????????????.h??????????????????????????????????.cpp?????????namespace?У????????????????????????????????????????????????е?const??????????????????????????ε??????????????????????????2?????????п??????const????????????2????????const???????????static??????????????????