Topic: about C + +, four types of conversion keywords, the detailed, can also give the code to determine the output or determine which code is wrong.
The answers and examples are as follows:
Four kinds of keywords: const_cast, constant transfer, dynamic_cast, downward security transition; Reinterpret_cast, to redefine the transformation; static_cast, static transformation;
1. Const_cast, constant removal:
The main variables of the constant (const) operation, remove the constant of variables, that is, can be very important to point and reference, see Code;
2. dynamic_cast, downward security transition:
It is mainly applied to the inheritance system, which can be transformed to "point to derived class" or "point sibling" by "Pointer to the base class part of the derived class".
Static_cast can only be converted to "point to derived classes";
3. reinterpret_cast, re-interpreting the transition:
The main reason is to redefine the 2 data (re-interpret), do not change the format, and static_cast will change the format for interpretation;
If the base class is converted by a derived class, the conversion is redefined, the address is changed, and the address is changed statically.
4. static_cast, static transformation:
It is primarily a conversion of data types and can also be used for inheritance;
The code is as follows:
* * * cppprimer.cpp * * Created on:2014.2.10 * author:spike//*eclipse CDT, gcc 4.8.1*/
#include <iostream>//* Constant Removal pointer detail/* struct S {s (): value (0) {} int value;
};
void Castconst (void) {const S;
Std::cout << "S.value =" << s.value << Std::endl; s* PS = &s;
Error, pointing to constant s* PS = const_cast<s*> (&s);
Ps->value = 1;
Std::cout << "S.value =" << s.value << Std::endl; s& rs = s;
Error, reference constant s& rs = const_cast<s&> (S);
Rs.value = 2;
Std::cout << "S.value =" << s.value << Std::endl; }/* Security downward transition/struct B/* Base class b*/{virtual void F () {std::cout << "base::f" << Std::endl;
} void Thisf () {std::cout << "BASE::THISF" << Std::endl;}
Virtual ~b () {}}; struct B2/* Base class b2*/{virtual void G() {std::cout << "base2::g" << Std::endl;}
void Thisg () {std::cout << "base2::thisg" << Std::endl;}
Virtual ~b2 () {}};
struct D:public B, public B2/* derived class d*/{virtual void F () {std::cout << "derived::f" << Std::endl;}
virtual void g () {std::cout << "derived::g" << Std::endl;}
Virtual ~d () {}};
void castdynamic (void) {b* pb_d = new D;
Pb_d->f (); Pd->g (); Error, only contains B part D *pd_d = dynamic_cast<d*> (pb_d);
Convert to derived class pd_d->g (); b2* pb2_d = dynamic_cast<b2*> (pb_d);
Convert to Brother Class Pb2_d->g ();
D *pd_ds = static_cast<d*> (pb_d);
Pd_ds->g (); b2* Pb2_ds = static_cast<b2*> (pb_d);
Error, cannot convert to sibling class}/* re-explain transformation/struct RA {int m_a;};
struct RB {int m_b;};
struct Rc:public RA, public RB {}; void Castreinterpret (void) {int *i= new int;
*i = 10;
Std::cout << "*i =" << *i << Std::endl;
Std::cout << "i =" << i << Std::endl;
Double *d=reinterpret_cast<double*> (i);
Std::cout << "*d =" << *d << Std::endl;
Std::cout << "d =" << D << Std::endl;
RC C; Std::cout << "&c =" << &c << std::endl << "reinterpret_cast<rb*>" ; c) = "<<reinterpret_cast<rB*> (&c) << std::endl <<" static_cast <rB*> (&am P;C) = "<< static_cast <rB*> (&c) << Std::endl <<" reinterpret_cast<ra*> (+ AMP;C) = "<<reinterpret_cast<rA*> (&c) << std::endl <<" static_cast <rA*> (
&C) = "<< static_cast <rA*> (&c) << Std::endl << Std::endl; int main (void) {Std::coUT << std::endl << "constant removal:" << Std::endl;
Castconst ();
Std::cout << Std::endl << "Security downward Transition:" << Std::endl;
Castdynamic ();
Std::cout << Std::endl << "re-Interpreting Transformation:" << Std::endl;
Castreinterpret (); }
Output:
Constant removal:
s.value = 0
s.value = 1
s.value = 2
Security downward transition:
derived::f
derived::g
derived::g
Derived::g
Transformation:
*i = ten
i = 0x471718
*d = 2.55917e-307
d = 0x471718 &c
= 0x22feb0< C15/>reinterpret_cast<rb*> (&c) = 0x22feb0
static_cast <rB*> (&c) = 0x22feb4
Reinterpret_cast<ra*> (&c) = 0x22feb0
static_cast <rA*> (&c) = 0x22feb0
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/