1. Type conversion
Static_cast<> (): Static type conversion, compile-time C + + compiler will do type checking, in C language, implicit type conversion, can use static_cast<> () for type conversion;
Reinterpret_cast<> (): coercion type conversion; compiler re-interpretation;
Dynamic_cast<cat *> (Base): Parent class object ===> sub-class object, downward transformation, generally used in inheritance;
Const_cast<> (): const char *---> char *, remove the constant attribute;
(1), the code is as follows:
#include <iostream>using namespace std;class Animal{ public: virtual void cry () = 0;}; class dog : public animal{ public: virtual void cry () { cout<< "Wang Wang" <<endl; } void dohome () { cout<< "housekeeping" <<ENDL;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;} private:};class Cat : public Animal{ public: virtual void cry () { cout<< "Meow" <<endl; } void dothing () { cout<< "Catch the Mouse" <<endl; } private :};void playobj (animal *base) { base->cry (); //1, inheritance 2, there is a virtual function of the rewrite 3, there is a parent pointer to a subclass object; ===> occurrence polymorphism //dynamic_cast can recognize sub-class object, runtime type recognition; Dog *pDog = dynamic_cast<Dog *> (base); //is its own type, will convert successfully, Otherwise the return is Null; if (Pdog) { pdog->dohome (); //let the dog do his peculiar work; } cat *pcat = Dynamic_cast<cat *> (base);//Parent Object ===> sub-class object, downward transformation; if (PCat) { pcat->dothing (); //let the cat do his peculiar work; }}int main (void) { &Nbsp; dog d1; cat c1; playobj (&D1); playobj (&C1); //animal *base = null; //base = static_cast<Animal *> (&D1); return 0;} /*int main (void) { double pi = 3.14; int num2 = static_cast<int> (pi); //static type conversion, compile-time C + + compiler will do type check, in C, implicit type conversion place, can use Static_ Cast<> () type conversion; char *p1 = "abcdef"; int *p2 = NULL; //p2 = static_cast<int*> (p1); // Use static_cast, compiler compile, will do type check, if there is error, prompt error; p2 = reinterpret_cast<int * > (p1);// coercion type conversion; compiler re-interpretation cout<<p1<<endl; cout<<p2<<endl; //%d return 0;} */
Operation Result:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8A/AF/wKiom1g3hiGwLAgUAAAzQM6uZlo269.png-wh_500x0-wm_3 -wmp_4-s_1166457831.png "title=" Qq20161125083019.png "alt=" Wkiom1g3higwlaguaaazqm6uzlo269.png-wh_50 "/>
(2), the type conversion code for Const_cast<char *> (p):
#include <iostream>using namespace std;void printbuf (const char *p) {//p[1] = ' 1 '; char *p1 = NULL; P1 = Const_cast<char *> (p);//const char *---> char *, remove the constant attribute; P1[0] = ' Z '; Cout<<p<<endl;} int main (void) {const char buf[] = "ABCDEFG"; Programmers want to make sure that the space that P points to can be modified, if not modified, will have disastrous consequences; const char *BUF = "abcdef";//A break error occurs because the space it points to itself cannot be modified printbuf (BUF); return 0;}
Operation Result:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8A/B0/wKiom1g3h4zwFFrUAAA1tomkDWg518.png-wh_500x0-wm_3 -wmp_4-s_2938937130.png "title=" Qq20161125083625.png "alt=" Wkiom1g3h4zwffruaaa1tomkdwg518.png-wh_50 "/>
This article is from the "wait0804" blog, make sure to keep this source http://wait0804.blog.51cto.com/11586096/1876440
Four types of load changer in C + +