????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 ???????????????????????????????б??????(????????????? ??????). ???????????????????????棻reinterpret_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?????????)??