/* Goal, C ++ type conversion. Although I usually don't use much, I am asked about the usage of each cast in the interview with C ++. Date, 2013-3-6env, ubuntu1204-gcc */# include <iostream> Using STD: cout; Using STD: Endl; // remove unreferenced warning template <typename T> void sinkunusedwarning (t) {} Class F {}; class G {public: // you cannot implicitly convert g (f) {}}; Class B {}; Class D: public B {}; void show (char * Str) {STR [0] = 'X'; cout <STR <Endl;} void show1 (const char * Str) {// you cannot change the const attribute. // STR [0] = 'y'; cout <STR <Endl;} int main () {// implicit conversion is disabled, built-in type Short F1 = 1; int G1 = F1; sinkunusedwarning (G1); // implicit conversion, class type F F2; G g2 = F2; sinkunusedwarning (G2 ); // explicit conversion, built-in type short F3 = 1; int G3 = (INT) F3; G3 = int (F3); sinkunusedwarning (G3); // explicit conversion, class D; B * pb; Pb = (B *) & D; sinkunusedwarning (PB); // static_castdouble F4 = 1.23456; int G4 = static_cast <int> (F4); sinkunusedwarning (G4); B; Pb = & B; // It is feasible to convert the base class into a derived class. It is not dangerous to convert the base class to a derived class? D * Pd = static_cast <D *> (PB); Pb = & D; // The derived class is converted to the base class. Feasible Pd = static_cast <D *> (PB ); sinkunusedwarning (PD); // dynamic_cast // The derived class is converted to the base class. The feasible Pb = dynamic_cast <B *> (& D); // The base class is converted to the derived class, unfeasible // Pd = dynamic_cast <D *> (& B); // reinterpret_castf * F5 = new F; G * G5; // The following conversion prompt is displayed, you cannot convert 'f * 'to 'G *'/G5 = F5; // when assigning a value. You cannot use reinterpret_cast. It seems dangerous to do so. G5 = reinterpret_cast <G *> (F5); sinkunusedwarning (G5); // const_cast, set or remove the const attribute of the variable. Const char STR [] = "ABC"; // The following example shows that the conversion from the 'const char * 'type to the 'Char *' type is invalid. // show (STR ); // when const_cast is used, the show (const_cast <char *> (STR); char str1 [] = "123"; show (str1); show1 (str1) prompt is not displayed during compilation ); show1 (const_cast <const char *> (str1); Return 0 ;}