C++_day9am
dynamic_cast
static_cast
reinterpret_cast
1 #include <iostream> 2 3 using namespace std; 4 5 class A{ 6 public: 7 //动态类型转换只能用于多态继承 8 virtual void foo(void){} 9 }; 10 11 class B: public A{}; 12 class C: public B{}; 13 class D{}; 14 15 int main(void) 16 { 17 B b; 18 A* pa = &b; //B is a A --皆然性 19 cout << "pa= " << pa << endl; 20 cout << "动态类型装换" << endl; 21 //pa实际指向B类对象,转换成功 22 B* pb = dynamic_cast<B*> (pa); 23 cout << "pb= " << pb << endl; 24 //pa没有指向C类对象,转换失败,安全 25 C* pc = dynamic_cast<C*> (pa); 26 cout << "pc= " << pc << endl; 27 try{ 28 A& ra = b; 29 C& rc = dynamic_cast<C&> (ra); 30 } 31 catch(exception& ex){ 32 cout << ex.what() << endl; 33 } 34 //pa没有指向D类对象,转换失败,安全 35 D* pd = dynamic_cast<D*> (pa); 36 cout << "pd= " << pd << endl; 37 cout << "静态类型转换" << endl; 38 //B是A的子类,转换成功 39 pb = static_cast<B*> (pa); 40 cout << "pb= " << pb << endl; 41 //C是A的孙子类,转换成功,危险 42 pc = static_cast<C*> (pa); 43 cout << "pc= " << pc << endl; 44 //D和A没有继承关系,转换失败,安全 45 //pd = static_cast<D*> (pa); 46 //cout << "pd= " << pd << endl; 47 cout << "重解释类型转换" << endl; 48 //编译期、运行期均不做检查,永远成功,最危险 49 pb = reinterpret_cast<B*> (pa); 50 cout << "pb= " << pb << endl; 51 pc = reinterpret_cast<C*> (pa); 52 cout << "pc= " << pc << endl; 53 pd = reinterpret_cast<D*> (pa); 54 cout << "pd= " << pd << endl; 55 56 return 0; 57 }
5.typeid.cpp
1 #include <iostream> 2 #include <typeinfo> 3 4 using namespace std; 5 6 namespace ns1{ 7 class A{ 8 public: 9 class B{ 10 public: 11 class C{}; 12 }; 13 }; 14 } 15 class Base{ 16 //virtual void foo(void){}; 17 }; 18 class Derived: public Base{}; 19 20 //class A{}; 21 22 int main(void) 23 { 24 int a = 0; 25 cout << typeid(int).name() << endl; //i 26 cout << typeid(unsigned int).name() << endl; //j 27 cout << typeid(char).name() << endl; //c 28 cout << typeid(unsigned char).name() << endl; //h 29 cout << typeid(short).name() << endl; //s 30 cout << typeid(unsigned short).name() << endl; //t 31 cout << typeid(long).name() << endl; //l 32 cout << typeid(long long).name() << endl; //x 33 cout << typeid(float).name() << endl; //f 34 cout << typeid(double).name() << endl; //d 35 cout << typeid(void).name() << endl; //v 36 cout << typeid(bool).name() << endl; //b 37 cout << typeid(int*).name() << endl; //Pi 38 cout << typeid(int**).name() << endl; //PPi 39 cout << typeid(int&).name() << endl; //i 40 cout << typeid(float[4]).name() << endl; //A4_f 41 cout << typeid(char*[4]).name() << endl; //A4_Pc 42 cout << typeid(char(*)[4]).name() << endl; //PA4_c 43 cout << typeid(short[2][3][4]).name() << endl; //A2_A3_A4_s 44 cout << typeid(char*(*)(short*, int*)).name() << endl; //PFPcPsPiE 45 46 struct Student{ 47 char name[128]; 48 int age; 49 }; 50 cout << typeid(Student).name() << endl; //Z4mainE7Student 51 cout << typeid(ns1::A::B::C).name() << endl; //N3ns11A1B1CE 52 53 Derived d; 54 Base* p = &d; 55 //基类中无虚函数 56 //cout << typeid(*p).name() << endl; //4Base 57 //Base& r = d; 58 //cout << typeid(r).name() << endl; //4Base 59 //基类中有虚函数:virtual void foo(void){}; 60 cout << typeid(*p).name() << endl; //7Derived 61 Base& r = d; 62 cout << typeid(r).name() << endl; 63 64 return 0; 65 }
6.shapes.cpp
1 #include <iostream> 2 #include <typeinfo> 3 4 using namespace std; 5 6 //形状:位置、绘制 7 //+--圆形:半径、(绘制) 8 //+--矩形:长宽、(绘制) 9 //形状 10 class Shape{ 11 public: 12 Shape(int x, int y): m_x(x), m_y(y){} 13 virtual void dummy(void){}; 14 void draw(void) const{}; //纯虚函数 15 16 protected: 17 int m_x; 18 int m_y; 19 }; 20 21 //圆形 22 class Circle: public Shape{ 23 public: 24 Circle(int x, int y, int r): Shape(x, y), m_r(r){} 25 void draw(void) const 26 { 27 cout << "圆形(" << m_x << ',' << m_y << ',' << m_r << ')' << endl; 28 } 29 private: 30 int m_r; 31 }; 32 33 //矩形 34 class Rectangle: public Shape{ 35 public: 36 Rectangle(int x, int y, int w, int h): Shape(x, y), m_w(w), m_h(h){} 37 void draw(void) const 38 { 39 cout << "矩形(" << m_x << ',' << m_y << ',' << m_w << ',' << m_h << ')' << endl; 40 } 41 private: 42 int m_w; 43 int m_h; 44 }; 45 46 void render(Shape* shapes[]) 47 { 48 for(size_t i = 0; shapes[i]; ++i) 49 if(typeid(*shapes[i]) == typeid(Circle)) 50 static_cast<Circle*> (shapes[i])->draw(); 51 else 52 if(typeid(*shapes[i]) == typeid(Rectangle)) 53 static_cast<Rectangle*> (shapes[i])->draw(); 54 } 55 56 void drawAny(Shape const& shape) 57 { 58 shape.draw(); 59 } 60 61 int main(void) 62 { 63 Shape* shapes[10] = {0}; 64 shapes[0] = new Circle (1,2,3); 65 shapes[1] = new Circle(4,5,6); 66 shapes[2] = new Rectangle(7,8,9,10); 67 shapes[3] = new Rectangle(11,12,13,14); 68 shapes[4] = new Circle(15,16,17); 69 render(shapes); 70 71 72 return 0; 73 }
7.dec.cpp
1 #include <iostream> 2 3 using namespace std; 4 5 class Base{ 6 public: 7 Base(void):m_array(new int[10]) 8 { 9 cout << "基类构造" << endl; 10 } 11 virtual ~Base(void) 12 { 13 cout <<"基类析构" <<endl; 14 delete[] m_array; 15 } 16 private: 17 int* m_array; 18 }; 19 20 class Derived: public Base{ 21 public: 22 Derived(void):m_buffer(new char[256]) 23 { 24 cout << "子类构造" << endl; 25 } 26 ~Derived(void) 27 { 28 cout << "子类析构" << endl; 29 delete [] m_buffer; 30 } 31 private: 32 char* m_buffer; 33 }; 34 35 int main(void) 36 { 37 Base* pb = new Derived; 38 delete pb; //在没有虚析构的情况下:基类函数不会调子类的析构函数 39 //使用虚析构后:调用的是子类的析构函数,该析构函数一方面析构子类特有的资源 40 //,另一方面还会自动调用基类的析构函数,最后连基类带子类所有的 41 //资源全部析构干净。 42 43 return 0; 44 } 45 //任何时候将基类的析构函数声明为虚函数,总不会有坏处
虚析构:
任何时候将基类的析构函数声明为虚函数,总不会有坏处