找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5727|回复: 2

[原]c++类型识别及转换

[复制链接]
发表于 2012-2-21 22:57:52 | 显示全部楼层 |阅读模式
1. 概念
RTTI(Run-Time Type Information)即运行时类型识别,c++通过RTTI实现对多态的支持。
c++是静态类型语言,其数据类型是在编译期就确定的,不能在运行时更改。
为了支持RTTI,C++提供了一个type_info类和两个关键字typeiddynamic_cast

type_info :
存储特点类型的相关信息,常用来比较对象类型,type_info类的具体内容由编译器实现来决定。其声明如下:
  1. class type_info {
  2. public:
  3.   virtual ~type_info();
  4.   bool operator== (const type_info& rhs) const;
  5.   bool operator!= (const type_info& rhs) const;
  6.   bool before (const type_info& rhs) const;
  7.   const char* name() const;
  8. private:
  9.   type_info (const type_info& rhs);
  10.   type_info& operator= (const type_info& rhs);
  11. };
复制代码
type_info的构造函数和赋值操作符都为私有,so,不要试图去定义或复制一个type_info对象,程序中创建type_info对象的唯一方法是使用typeid操作符需要特别注意的是,c++标准只是告诉编译器需要实现type_info::name函数,但是不同的编译器实现各不相同,这意味着:typeid(int).name()不同编译器编译运行后输出不一样。

typeid:

typeid表达式形如:typeid(expr);
typeid返回type_info类型,expr可以是各种类型名,对象和内置基本数据类型的实例、指针或者引用,当作用于指针和引用将返回它实际指向对象的类型信息。
如果表达式的类型是类类型且至少包含有一个虚函数,则typeid操作符返回表达式的动态类型,需要在运行时计算;否则,typeid操作符返回表达式的静态类型,在编译时就可以计算。
当把typeid作用于指针的解引用*p时,若指针p为0,则:如果p指向的类型是带虚函数的类类型,则typeid(*p)在运行时抛出一个bad_typeid异常;否则,typeid(*p)的结果与p的值是不相关的,在编译时就可以确定

dynamic_cast:
动态类型转换,运行时类型安全检查。dynamic_cast会检查待转换的源对象是否真的可以转换成目标类型,这种检查不是语法上的,而是真实情况的。许多编译器都是通过vtable找到对象的RTTI信息的,如果基类没有虚方法,也就无法判断一个基类指针变量所指对象的真实类型。
dynamic_cast将一个指向基类的指针转换为一个指向派生类的指针,如果不能正确转换,则返回空指针。

2. 其它类型转换符
除了dynamic_cast,标准C++中还有其它三个类型转换符:static_cast、reinterpret_cast和const_cas。
和dynamic_cast不一样,其它三个类型转换符行为都是在编译期就得以确定的,转换是否成功,并不依赖被转换的对象。
const_cast:去掉类型的const或volatile(告诉编译器不要持有变量的临时拷贝)属性。
reinterpret_cast :重新解释类型,常用于不同类型的指针类型转换用。
static_cast:类似于C风格的强制转换,静态类型转换。

3.实例
代码:
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4. struct V_Base
  5. {
  6.   virtual ~V_Base()
  7.   {
  8.   }
  9. };
  10. struct V_Derived : V_Base
  11. {
  12. public:
  13.         int _a;
  14. };
  15. struct Base
  16. {
  17.   ~Base()
  18.   {
  19.   }
  20. };
  21. struct Derived : Base
  22. {
  23. public:
  24.         int _a;
  25. };
  26. int main() {
  27.   // built-in types:
  28.   int i;
  29.   int * pi;
  30.   cout << "i is: " << typeid(i).name() << endl;
  31.   cout << "pi is: " << typeid(pi).name() << endl;
  32.   // polymorphic types:
  33.   V_Derived v_derived;
  34.   V_Base* v_pbase = &v_derived;
  35.   cout << "v_derived is: " << typeid(v_derived).name() << endl;
  36.   cout << " *v_pbase is: " << typeid(*v_pbase).name() << endl;
  37.   cout << boolalpha << "same type? ";
  38.   cout << ( typeid(v_derived)==typeid(*v_pbase) ) << endl << endl;
  39.   
  40.   // non polymorphic types:
  41.   Derived derived;
  42.   Base* pbase = &derived;
  43.   cout << "derived is: " << typeid(derived).name() << endl;
  44.   cout << " *pbase is: " << typeid(*pbase).name() << endl;
  45.   cout << boolalpha << "same type? ";
  46.   cout << ( typeid(derived)==typeid(*pbase) ) << endl << endl;
  47.         //polymorphic dynamic_cast
  48.         V_Base* v_pb1 = new V_Derived();
  49.         V_Derived *v_pd1 = static_cast<V_Derived *>(v_pb1); //子类->父类,静态类型转换,正确但不推荐
  50.         V_Derived *v_pd2 = dynamic_cast<V_Derived *>(v_pb1); //子类->父类,动态类型转换,正确
  51.         if(v_pd2 == NULL)
  52.         {
  53.                 cout << "v_pd2 dynamic_cast failed" << endl << endl;
  54.         }
  55.         V_Base* v_pb2 = new V_Base();
  56.         V_Derived *v_pd3 = static_cast<V_Derived *>(v_pb2); //父类->子类,静态类型转换,危险!访问子类_a成员越界
  57.         V_Derived *v_pd4 = dynamic_cast<V_Derived *>(v_pb2); //父类->子类,动态类型转换,安全的,结果是NULL
  58.         if(v_pd4 == NULL)
  59.         {
  60.                 cout << "v_pd4 dynamic_cast failed" << endl << endl;
  61.         }
  62.         /*
  63.         //non polymorphic dynamic_cast
  64.         Base* pb1 = new Derived();
  65.         Derived *pd1 = static_cast<Derived *>(pb1);
  66.         Derived *pd2 = dynamic_cast<Derived *>(pb1); //编译错误:error: cannot dynamic_cast ‘pb1’ (of type ‘struct Base*’) to type ‘struct Derived*’ (source type is not polymorphic
  67.         if(pd2 == NULL)
  68.         {
  69.                 cout << "pd2 dynamic_cast failed" << endl << endl;
  70.         }
  71.         Base* pb2 = new Base();
  72.         Derived *pd3 = static_cast<Derived *>(pb2);
  73.         Derived *pd4 = dynamic_cast<Derived *>(pb2); //编译错误:error: cannot dynamic_cast ‘pb2’ (of type ‘struct Base*’) to type ‘struct Derived*’ (source type is not polymorphic)
  74.         if(pd4 == NULL)
  75.         {
  76.                 cout << "pd4 dynamic_cast failed" << endl << endl;
  77.         }
  78.         */
  79. }
复制代码
gcc编译,运行输出:
i is: i
pi is: Pi
v_derived is: 9V_Derived
*v_pbase is: 9V_Derived
same type? true

derived is: 7Derived
*pbase is: 4Base
same type? false

v_pd4 dynamic_cast failed
作者:yfkiss 发表于2012-2-21 22:24:11 原文链接


发表于 2013-1-10 23:37:42 | 显示全部楼层
学习了,以前也涉及到泛型之类的东东,但是不是很了解,现在有思路了
发表于 2013-2-16 09:38:34 | 显示全部楼层
这些转换函数会让初学者很晕很晕的
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-4-20 19:16 , Processed in 0.022637 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表