Dynamic_cast 1.0 converts a pointer or reference to a base class safely to a pointer or reference to another derived class. 2.0 Note that the dynamic_cast converter can only be used for classes that have virtual functions, and its expression is dynamic_cast< type >(expression). 3.0 If dynamic_cast fails to convert the pointer type, 0 is returned; If the conversion to the reference type fails, an exception of type BAD_cast is thrown.

Typeid // Swaps back the type to which the pointer or reference points

Type conversion operation static_cast, of the form static_cast< target datatype >(source datatype expression) for example: int a; static_cast(a); Equivalent to: (double)a; Const_cast converts a const pointer to a nonconst pointer.

typeinfo

Auto: When the compiler can infer the type of a variable at the time it is declared before it gets a vector iterator vector vec; vector::iterator ite=vec.iterator(); Now the vector vec. auto ite=vec.iterator();


      

! [dynamic_cast.PNG](http://upload-images.jianshu.io/upload_images/4976516-95819518e064f2df.PNG? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ! [const_cast.PNG](http://upload-images.jianshu.io/upload_images/4976516-b9000d28e0ba9b78.PNG? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ! [static_cast.PNG](http://upload-images.jianshu.io/upload_images/4976516-554ecef6e4ba4a89.PNG? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ! [typeid.PNG](http://upload-images.jianshu.io/upload_images/4976516-9eb326514a0104c9.PNG? ImageMogr2 /auto-orient/strip% 7cimageView2/2 /w/1240) >

Copy the code

#include #include using namespace std;

class A { protected: string name; public: A(string name); virtual void display(); };

A::A(string name) { this->name = name; cout << “construct A” << endl; }

void A::display() { cout << “my name is” << name << endl; }

class B:public A { public: B(string name,string n); void display(); };

B::B(string name, string n):A(n) { this->name = name; cout << “construct B” << endl; } void B::display() { cout << “my name is” << name << endl; }

int main() { /* B b(“B”, “A”); A *p0 = &b; p0->display(); A *p = new B(“B”,”A”); B pb = dynamic_cast(p); // Conversion security. //dynamic_class requires polymorphic if (pb! = NULL) {// polymorphic call B inherits A constructor pb->display(); // Only a pointer to a parent class points to a child class or a reference to a parent class. } else cout << “Conversion is not safe, exit” << endl;

A *pa = new A("A"); B *p2 = dynamic_cast<B*>(pa); if (p2! =NULL) { p2->display(); } else cout << "Conversion is not safe, exit" << endl; cout << "----------------------" << endl;Copy the code

/ / cout < < “” DFLX typeid judgment data type” type: “< < the typeid (” DFLX”). The name () < < endl; Cout <<“6 type: “<< typeId (6).name() << endl; //const_cast, const int a = 66; // const int *b = &a; // the unit referred to by b cannot be overwritten. (const_cast

(b)) = 88; / / remove const attribute cout < < “a =” < < < < endl < < a * b * b = “” < < < < endl; cout << “———————-” << endl; //static_cast cout << static_cast(9.999) << endl; cout << “———————-” << endl; //auto tells the compiler to determine the data type auto c = 22; //c is an int. char ch[] = { ‘a’,’b’,’c’ }; For (auto c: ch) cout << c << “”; cout << endl; //decltype, obtain an expression type.

system("pause");
return 0;
Copy the code

}


      
Copy the code

Construct A construct B my name isB Construct A conversion is not safe, exit

“DFLX” type:char const [5] 6 type: int a=88 *b=88

9

a b c

>I still have a cold in October. Sometimes I cough and cry. It seems that I still need to strengthen my physical exercise.< HR > [c + + RTTI mechanism analysis] (http://blog.csdn.net/pi9nc/article/details/21742355) [four types in c + + transformation way] (http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/23/2652696.html) [compare methods of transformation of the four types in c + +] (http://blog.csdn.net/hrbeuwhw/article/details/7884797) [c + + 11 New features of the automatic type inference and obtain] (http://blog.csdn.net/kaitiren/article/details/22302767)Copy the code