С??C++??const????????
???????????? ???????[ 2012/12/4 10:14:57 ] ????????
????????
????C???????C++?????е?const??????????????????????????????????????????????????????????Щ????????????const???????????????
???????????
?????????const??C?????б????????????????C++?????б????????????const??C??C++?????е???????????????????????????????????
???????const???????????????? ?????????const???????????????int??short??long??????????????????const??????????????????????????????????????????????????????????????????????????????????????????С???????????Щ????
????C????const?????
const int i=10;
int *p=(int *)(&i);
*p=20;
printf("i=%d *p=%d
"??i??*p);
?????????????????????i=20 *p=20
????C++????const???1??
const int i=10;
int *p=const_cast<int *>(&i);
*p=20;
cout<<"i="<<i<<" *p="<<*p<<endl;
???????????? i=10 *p=20
????C++????const???2??
struct test{
int j;
char tmp;
test()
{
j=30;
tmp='a';
}
};
int main(int argc?? char* argv[])
{
const struct test t1;
int *q=(int *)(&t1.j);
*q=40;
cout<<"j="<<t1.j<<" *q="<<*q<<endl;
return 0;
}
???????????? j=40 *q=40
??????????????
????????????????????????????ие??????
????????1??????const int????????i??C????????????p????????i?????20??????C++?У???????p????????i?????10??
????????2??C++?????? const struct test?????j??????q???????????const int ?? const struct test????????????
???????????1?????????C??????const??????????????????const??????????????????????л??д洢??????????????????????????????????????????p????????е????????i??????????????????????????????????C++??const????????????????????ó???????滻???i???????????cout<<i; ??????cout<<10; ???????????i???????????????????е?????C????????#define i 10?????C++??i?????10????*p?????20.
???????????2??C++???????????????????????????????滻?????????????????????????????????????????????????????????????????????????????????滻??????????????????????????????????????????????????q?????????????????C++??const int?????????????????????
????С??
????C??????C++?к??????????????const?????????????棬????const???????????????????????????????
??????
???·???
??????????????????
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