C++ const???????
???????????? ???????[ 2012/8/15 14:35:55 ] ????????
??????????壬const???ε??????????????????
??????C++??????????к?????????????
????1??????????????????????????
????2?????????????????????????????
????3???Щ???????????????????????????????????????
????4????????????ù?????ж???????????????
??????const???ε??????????????μ???????
????1??????????????????????????????????
????2????????const???????constant???????????????????????????????????????洢???
????????????????????????????????????????????
?????????????????????????????????????????????????????????????????С?
?????????????????????????????????С???const?????y??????????????????Щ??????????
?????????????????м??????
// ???????????????
int val = 2012;
int& valr = val; // ok
int& valr1 = &val; // error
int* p = &val;
int*& valr2 = &p; // ok??valr2??int*?????????
// ?????????????ú???????const T&??T&
int val = 2012;
int& valr = val; // ok
const int& valr1 = val; // ok
int& valr = 2012; // error?????????T&?????????????????????2012????????????????
const int& valr1 = 2012; // ok?????????int temp = int(2012);???????temp??????????temp?????const int&?????temp??????????????????
// ????????????????
int val = 2012;
int& valr = val+1; // error?????val+1????????????????????????????int&
const int& valr1 = val+1; // ok?????????м?????????????
// ?????y?????????
int val = 2012;
int*& valr = &val; // error?????&val????????????&val??????????????????????&val++???????
int* const& valr = &val; // ok??const T&??????????????????&val?????м????????????????????const???????*???????????????????????????
const int var = 2012;
int*& varr = &var; // error???????
int* const& varr = &var; // error?????var??const int?????
const int* const& varr = &var; // ok?????????
// ???????const&
const int* p = &var;
const int*& varr = p; // ok??????????const&?????p?????
// ?????const??????????const&
// ??????Щconst???????????????????????????洢??????????????????????
const int val = 2012; // val??????洢???
int& valr = val; // error??val?????????
const int& valr = val; // ok?????????м????????
// ?????????????
const int val = 2012;
const int* p = &val;
const int* &pr = p; // ok??p?????
const int* const& pr1 = &val; // ok??????м?????????
int* const& pr2 = &val; // error??????????????????????const int*?????????int*
// const&??????????????
int val = 2012;
const double& valr = val;
double dval = 3.14159;
const int& valr = dval; // ok??temp????????????????int temp = int(dval);
const int& valr1 = 3.14159; // ok??temp????????????????int temp = int(3.14159);
int& valr2 = dval; // error?????temp?????м?????????
int& valr3 = 3.14159; // error???????????м???????????????
??????
???·???
??????????????????
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