????5?????
??????1??dynamic_cast vs static_cast
????class B
????{
????...
????};
????class D : public B
????{
????...
????};
????void f(B* pb)
????{
????D* pd1 = dynamic_cast<D*>(pb);
????D* pd2 = static_cast<D*>(pb);
????}
????If pb really points to an object of type D?? then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.
????If pb points to an object of type B and not to the complete D class?? then dynamic_cast will know enough to return zero. However?? static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.
??????dynamic_cast????????????械???????????????????????????????????static_cast???????????dynamic_cast?????效?????static_cast????些????static_cast????????围??????????????????????????????????????static_cast?????浠�????????蔚??????????????????????浠�??????浠�(????浠�??????????????????)????VOID*?????浠�?????????浠�??…
??????2??static_cast vs reinterpret_cast
????reinterpret_cast???????????????????????????????????????????????????????????????????????????????????????????奴??????????????????????????危????(??浠�??C++???????械????)
????static_cast ?? reinterpret_cast ????????????????????????????????? static_cast ???????????????????????????????斜??????(????????????? ??????). ???????????????????????妫籸einterpret_cast ?????????????????????????????????薪??卸?????????? ???????锟�?
????int n=9; double d=static_cast < double > (n);
??????????????校? ?????????????? int ????? double?? ??些???????????????????? ??????? 9 ????? ????????? 9??static_cast ?????????????????? d ???????位??????? 9.0??
??????reinterpret_cast ?????????:
????int n=9;
????double d=reinterpret_cast<double & > (n);
??????危? ??????????. ????屑?????? d ?????????. ??????? reinterpret_cast ????????? n ?????位?? d?? ??薪??斜???????.
???????? ???????????? reinterpret_cast.
?????????http://www.cppblog.com/lapcca/archive/2010/11/30/135081.aspx
????????
??????1??static_cast????????????????C??????????????????????????????泄??????????????纾�??????static_cast????C???????????struct?????int????????double?????????????????????static_cast?????????????const??????????????渭??????????const_cast????????????
???????????????????????????????????????????校??????????????泻????校?????????芯???????????????
??????2??const_cast????????????????????const??volatile???????????const_cast???????????????????????????????????????????些??????constness????volatieness???????????灞�????????????????????????const_cast????????constness????volatileness???????????椋�???????????????????
??????3??dynamic_cast??????????????????????泄???????????????????????????????dynamic_cast???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????y???????????????
??????4??reinterpret_cast????????????????????????????????????????????????濉�???????reinterpret_cast?????????????reinterpret_casts???????????????????????????????????
???????????锟�?
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo()
{
}
};
class B
{
public:
virtual void foo()
{
}
};
class C : public A ?? public B
{
public:
virtual void foo() { }
};
void bar1(A *pa)
{
B *pc = dynamic_cast<B*>(pa);
}
void bar2(A *pa)
{
B *pc = static_cast<B*>(pa); //error
}
void bar3()
{
C c;
A *pa = &c;
B *pb = static_cast<B*>(static_cast<C*>(pa));
}
int main()
{
return 0;
}
A??bar1??????????
B??bar2??????????
C??bar3??????????
D??bar1???????????校??????????????cast????
???????
?????B??
????dynamic_cast?????????????????????????????????????????????A??B????????????????????(????A??D????????)??
????static_cast?????????????械??魏?????????????????????伞????卸??????1?????????????????int????double(?????????????????)??????????int*?????????double*????写?????????????2???????????????????????????????????????????B???????????????泄????????????????????????????魏???????????????????????bar3???????????C?????????)??