C++??????????????????????
???????????? ???????[ 2016/12/14 13:55:05 ] ??????????????????? ??? ????
?????????C++?????????????static_cast??dynamic_cast??reinterpret_cast??const_cast??
????1??static_cast
?????÷???static_cast (expression)
?????????????expression????type-id???????????????????????????????????????????????????÷???
??????1?????????ν??л?????????????????????????
????????????????????????????????????????????????????
?????????????????????????????????????????????????????ж???????飬????????????
??????2?????????????????????????????int?????char???????????????????????????
??????3?????????????????????????
??????4?????κ?????????????void????
???????static_cast?????????expression??const??volitale????__unaligned?????
????2??dynamic_cast
?????÷???dynamic_cast (expression)
?????????????expression?????type_id????????type_id???????????????????void*??
???????type_id???????????????expression????????????????type_id?????????????expression?????????????á?
????dynamic_cast??????????μ????????????????????????????????????????????
?????????μ??????????????dynamic_cast??static_cast??Ч??????????
???????????????????dynamic_cast?????????????????static_cast???????
class B
{
public:
int m_iNum;
virtual void foo();
};
class D:public B
{
public:
char *m_szName[100];
};
void func(B *pb)
{
D *pd1 = static_cast<D *>(pb);
D *pd2 = dynamic_cast<D *>(pb);
}
????????????????У????pb??????D????????pd1??pd2?????????????????????????D??????κβ????????????
????????????pb?????????B???????????pd1??????????????????????????D????????????????????????m_szName??????pd2????????????
????????????B????麯????????????????static_cast??????????????????????????????????????????????????????????洢??????麯??????????麯?????????????????У???ж??????麯??????????麯???????ж????麯????????????麯??????
????????dynamic_cast??????????????????????
class A
{
public:
int m_iNum;
virtual void f(){}
};
class B:public A
{
};
class D:public A
{
};
void foo()
{
B *pb = new B;
pb->m_iNum = 100;
D *pd1 = static_cast<D *>(pb); //compile error
D *pd2 = dynamic_cast<D *>(pb); //pd2 is NULL
delete pb;
}
?????????foo?У????static_cast????????????????????????????????????dynamic_cast??????????????????????
????3??reinterpret_cast
?????÷???reinterpret_cast (expression)
????type-id???????????????á?????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????÷?????
????(static_cast .??. reinterpret_cast?????????? )
????????????????????
????4??const_cast
?????÷???const_cast (expression)
???????????????????????const??volatile?????????const ??volatile??????? type_id??expression??????????????
?????????????????????????????????????????
??????????????????????????????????????????????????????????????????
????volatile??const??????????????????
????class B
????{
????public:
????int m_iNum;
????}
????void foo()
????{
????const B b1;
????b1.m_iNum = 100; //comile error
????B b2 = const_cast<B>(b1);
????b2. m_iNum = 200; //fine
????}
????????????????????????b1???????????????????????и??
???????const_cast?????????????????????????????????????????????b1??b2??????????????
??????
???·???
??????????????????
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