????4.       Const_cast ???????????????????const??volatile?????????const ??volatile??????? type_id??expression??????????????
????????????????????????????????????????????
?????????????????????????????????????????????????
?????????????????????????????
????Voiatile??const????????????????
class B
{
public:
int m_iNum;
B(){}
};
void foo()
{
const B b1;
//b1.m_iNum = 100; //compile error
B& b2 = const_cast<B&>(b1);
b2. m_iNum = 200; //fine?
}
int main()
{
foo();
return 0;
}
??????????????????????b1???????????????????????и??
???????const_cast?????????????????????????????????????????????b1??b2??????????????
????c/c++??????????
????Q:????C????????????static_cast?? dynamic_cast ??? reinterpret_cast???????????????????
????A:????????????????????????????????????????????????????????????????????????????????????????????????????????????????磬??????????????doubole?????????????????
????????:
????int i;
????double d;
????i = (int) d;?????i = int (d);
?????? ????б?????????????????????????á??????????????????????????????????class??????????ANSI-C++?????????????μ? ???????'reinterpret_cast'?? 'static_cast'?? 'dynamic_cast' ?? 'const_cast'??????????????(class)?????????????
????????:
????reinterpret_cast<new_type>(expression)
????dynamic_cast<new_type>(expression)
????static_cast<new_type>(expression)
????const_cast<new_type>(expression)
????1 reinterpret_cast
????reinterpret_cast ????????????????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????κ?????????????????????????????????????????????????????????????κε?????????????????????????????????????????????????????????Ч???????
????????:
????class A {};
????class B {};
????A * a = new A;
????B * b = reinterpret_cast<B *>(a);
????reinterpret_cast  ???????????????????????????????????
????2 static_cast
????static_cast ???????????????????????????????????????????????????
?????????????????????????????????????????????????????Ч???????????????????????????????????????????????
???????????????????????????б??????????????????????
????????
????class Base {};
????class Derived : public Base {};
????Base *a    = new Base;
????Derived *b = static_cast<Derived *>(a);
????static_cast  ??????????????????????????????????????????????????????????????:
????????:
????double d = 3.14159265;
????int    i = static_cast<int>(d);
????3 dynamic_cast
????dynamic_cast ????????????????á???????????????????????????????????????????????????????static_cast????????????????????????? ?????????????dynamic_cast????????????Ч???????????????????????????????????Ч??????????
?????????????????С????????????????????????????Ч??????????????????NULL.
????????
????class Base { virtual dummy() {} };
????class Derived : public Base {};
????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??????????????
????????:
????class Base { virtual dummy() {} };
????class Derived : public Base { };
????Base* b1 = new Derived;
????Base* b2 = new Base;
????Derived d1 = dynamic_cast<Derived &*>(b1);          // succeeds
????Derived d2 = dynamic_cast<Derived &*>(b2);          // fails: exception thrown
????4 const_cast
????????????????????????const?????????????????????????
????????:
????class C {};
????const C *a = new C;
????C *b = const_cast<C *>(a);
????????????????????????????????????????
???????'const_cast'?????????????volatile qualifier??
????C++??????????????????????????????????
????·dynamic_cast ??????????“?????????????safe downcasting??”??????????????????????????????????е????????????????????t???????е????????????????????????????????????
????·static_cast ????????????????????????磬non-const ???????? const ????int ???? double????????????????????????????????????????????磬void* ???????????????????????????????????????????????????? const ???????? non-const ??????? const_cast ?????????????????C-style???????
????·const_cast ????????????????????????????????????????? C++ ???????????
????·reinterpret_cast ?????????????????????????????????implementation-dependent???????????????????????????磬??????????????????????????????????????????????ü????????
????C??????????????(Type Cast)????????????????????????
????TYPE b = (TYPE)a
????C++???????????????4?????????????????????????????á?
????const_cast??????????????const?????
????static_cast??????????????????????????int?????char??
????dynamic_cast?????????????????????????????????????????????????
????reinterpreter_cast??????????????????????н??ж???????????
????4????????????????磺
????TYPE B = static_cast(TYPE)(a)
????const_cast
????????????const??volatile?????
????struct SA {
????int i;
????};
????const SA ra;
????//ra.i = 10; //??????const????????????
????SA &rb = const_castSA&>(ra);
????rb.i = 10;
????static_cast
??????????C????????????????????????????????????????
????1. ?????????????????????????????????????????????;??????????????????????????????(????????????????????????????dynamic_cast)
????2. ?????????????????enum?? struct?? int?? char?? float???static_cast??????????????(???????????)????????????
????3. ???????????????????????
????4. ???κ??????????????void?????
????5. static_cast????????????const??volitale????(??const_cast)??
????int n = 6;
????double d = static_castdouble>(n); // ???????????
????int *pn = &n;
????double *d = static_castdouble *>(&n) //??????????????????????
????void *p = static_castvoid *>(pn); //?????????????void????
????dynamic_cast
?????????????????????????????????????????(?????????NULL)??
????1. ?????????????????????
????2. ????????麯????
????3. ???????????????????????????????NULL??
class BaseClass {
public:
int m_iNum;
virtual void foo(){};
//??????????麯????????????????????dynamic_cast
};
class DerivedClass: public BaseClass {
public:
char *m_szName[100];
void bar(){};
};
BaseClass* pb = new DerivedClass();
DerivedClass *pd1 = static_castDerivedClass *>(pb);
//????->??????????????????????????
DerivedClass *pd2 = dynamic_castDerivedClass *>(pb);
//????->???????????????????
BaseClass* pb2 = new BaseClass();
DerivedClass *pd21 = static_castDerivedClass *>(pb2);
//????->????????????????Σ???????????m_szName??????
DerivedClass *pd22 = dynamic_castDerivedClass *>(pb2);
//????->???????????????????????????NULL
reinterpreter_cast
????????????????????????н??ж???????????
????1. ???????????????????????á?????????????????????????
????2. ?????λ?????????????????????????????????????????????????????????????????(?????????????????????????????????????????????????????????????)???????????32bit???????????
????3. ?????????????????????????????????
????4. ????????????
int doSomething(){return 0;};
typedef void(*FuncPtr)();
//FuncPtr is ??????????????ú?????в??????????????? void
FuncPtr funcPtrArray[10];
//10??FuncPtrs???????? ????????????????????Щī?????????????????????溯??????????funcPtrArray???飺
funcPtrArray[0] = &doSomething;
// ???????????????reinterpret_cast?????????????????????????????funcPtrArray
funcPtrArray[0] = reinterpret_castFuncPtr>(&doSomething);
//???????????????????????
?????? ??
?????const??????const_cast??
?????????????????static_cast??
??????????????????????daynamic_cast??
????????????????????????reinterpreter_cast??