typeidRTTI.cpp;dynamic_cast<Derive22&>(b2);
来源:互联网 发布:cad软件许可管理器 编辑:程序博客网 时间:2024/04/30 03:29
// typeidRTTI.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>using namespace std;int main( void ){// sample 1 cout << typeid(1.1f).name() << endl;// sample 2 class Base1 { }; class Derive1 : public Base1 { }; Derive1 d1; Base1& b1 = d1; cout << typeid(b1).name() << endl; // 输出"class Base1",因为Derive1和Base1之间没有多态性// sample 3, 编译时需要加参数 /GR class Base2 { virtual void fun( void ) {} }; class Derive2 : public Base2 { }; Derive2 d2; Base2& b2 = d2; cout << typeid(b2).name() << endl; // 输出"class Derive2",因为Derive1和Base1之间有了多态性// sample 4 class Derive22 : public Base2 { }; // 指针强制转化失败后可以比较指针是否为零,而引用却没办法,所以引用制转化失败后抛出异常 Derive2* pb1 = dynamic_cast<Derive2*>(&b2); cout << boolalpha << (0!=pb1) << endl; // 输出"true",因为b2本身就确实是Derive2 Derive22* pb2 = dynamic_cast<Derive22*>(&b2); cout << boolalpha << (0!=pb2) << endl; // 输出"false",因为b2本身不是Derive2 try { Derive2& rb1 = dynamic_cast<Derive2&>(b2); cout << "true" << endl; } catch( bad_cast ) { cout << "false" << endl; } try { Derive22& rb2 = dynamic_cast<Derive22&>(b2); cout << "true" << endl; } catch( ... ) // 应该是 bad_cast,但不知道为什么在VC++6.0中却不行 { cout << "false" << endl; } return 0;}