C++?е?const??constexpr
???????????? ???????[ 2016/1/5 11:35:51 ] ????????.NET ???????????
????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??????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11