C++????cast??????
???????????? ???????[ 2014/4/15 15:52:20 ] ????????net ???? ???????
???????????????????????????????????????????????????upercasting?????????Ч?????????????????????????????????????????????downcasting??????????????????????????????????????????????????У???
class Base {};
class Derived : public Base {};
Base *a = new Base;
Derived *b = NULL;
b = static_cast<Derived *>(a); //???????????????????????(?????//Derived????)
|
???????
????1??static_cast?????????expression??const??volitale??????__unaligned?????
????2?????????????????????У?????????????????????????????????£?????????????????????????????????????????±???????
????1.4 dynamic_cast
????????????????????????????????“????????????”??????????????????????????????????е????????????????????t???????е????????????????????????????????????
????????????????????????麯?????????????????????????????????????????????static_cast???????????????????????????????????dynamic_cast????RTTI??????????????Ч??????????dynamic_cast???????????????????????????Ч???????????????鯔?????????????????????顣?????????????У????????????????????????????Ч??????????????????NULL??
???????RTTI???????????????????????????vtable????????RTTI???????????ζ??????????????麯?????????ж?????????????????????????????? ?????dynamic_cast???????????????????upercasting??????????????????????????????????????????????dynamic_cast????
class Base { virtual dummy() {} };
class Derived : public Base {};
class Other{} ;
Base* b1 = new Derived;
Base* b2 = new Base;
Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds
Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'
//?????????????????????????????????????????????????????bad_cast??????????????
Derived d3 = dynamic_cast<Derived &>(*b1); // succeeds
Derived d4 = dynamic_cast<Derived &>(*b2); // fails: exception thrown
|
???????Base??????麯????????????????
????1.5 С??
????reinpreter_cast??????“???????”???????????const_cast????????const??volatile????????????????
????static_cast????????Χ???????????????????????????????????????????μ??????????????dynamic_cast??static_cast??Ч?????????????????????????????????????麯??????dynamic_cast?????????????????????Ч???????static_cast?????
??????
???·???
??????????????????
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