c++类型识别及转换

1. 概念
RTTI(Run-Time Type Information)即运行时类型识别,c++通过RTTI实现对多态的支持。
c++是静态类型语言,其数据类型是在编译期就确定的,不能在运行时更改
为了支持RTTI,C++提供了一个type_info和两个关键字typeiddynamic_cast


type_info :
存储特点类型的相关信息,常用来比较对象类型, type_info类的具体内容由编译器实现来决定。其声明如下:
class type_info {
public:
  virtual ~type_info();
  bool operator== (const type_info& rhs) const;
  bool operator!= (const type_info& rhs) const;
  bool before (const type_info& rhs) const;
  const char* name() const;
private:
  type_info (const type_info& rhs);
  type_info& operator= (const type_info& rhs);
};
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.实例
代码:
#include <iostream>
#include <typeinfo>
using namespace std;

struct V_Base 
{
  virtual ~V_Base()
  {
  }
};

struct V_Derived : V_Base 
{
public:
	int _a;
};

struct Base
{
  ~Base()
  {
  }
};

struct Derived : Base
{
public:
	int _a;
};

int main() {
  // built-in types:
  int i;
  int * pi;
  cout << "i is: " << typeid(i).name() << endl;
  cout << "pi is: " << typeid(pi).name() << endl;

  // polymorphic types:
  V_Derived v_derived;
  V_Base* v_pbase = &v_derived;
  cout << "v_derived is: " << typeid(v_derived).name() << endl;
  cout << " *v_pbase is: " << typeid(*v_pbase).name() << endl;
  cout << boolalpha << "same type? "; 
  cout << ( typeid(v_derived)==typeid(*v_pbase) ) << endl << endl; 
  
  // non polymorphic types:
  Derived derived;
  Base* pbase = &derived;
  cout << "derived is: " << typeid(derived).name() << endl;
  cout << " *pbase is: " << typeid(*pbase).name() << endl;
  cout << boolalpha << "same type? "; 
  cout << ( typeid(derived)==typeid(*pbase) ) << endl << endl;

	//polymorphic dynamic_cast
	V_Base* v_pb1 = new V_Derived();
	V_Derived *v_pd1 = static_cast<V_Derived *>(v_pb1); //子类->父类,静态类型转换,正确但不推荐
	V_Derived *v_pd2 = dynamic_cast<V_Derived *>(v_pb1); //子类->父类,动态类型转换,正确
	if(v_pd2 == NULL)
	{
		cout << "v_pd2 dynamic_cast failed" << endl << endl;
	}
	V_Base* v_pb2 = new V_Base();
	V_Derived *v_pd3 = static_cast<V_Derived *>(v_pb2); //父类->子类,静态类型转换,危险!访问子类_a成员越界
	V_Derived *v_pd4 = dynamic_cast<V_Derived *>(v_pb2); //父类->子类,动态类型转换,安全的,结果是NULL
	if(v_pd4 == NULL)
	{
		cout << "v_pd4 dynamic_cast failed" << endl << endl;
	}

	/*
	//non polymorphic dynamic_cast
	Base* pb1 = new Derived();
	Derived *pd1 = static_cast<Derived *>(pb1); 
	Derived *pd2 = dynamic_cast<Derived *>(pb1); //编译错误:error: cannot dynamic_cast ‘pb1’ (of type ‘struct Base*’) to type ‘struct Derived*’ (source type is not polymorphic
	if(pd2 == NULL)
	{
		cout << "pd2 dynamic_cast failed" << endl << endl;
	}
	Base* pb2 = new Base();
	Derived *pd3 = static_cast<Derived *>(pb2);
	Derived *pd4 = dynamic_cast<Derived *>(pb2); //编译错误:error: cannot dynamic_cast ‘pb2’ (of type ‘struct Base*’) to type ‘struct Derived*’ (source type is not polymorphic)
	if(pd4 == NULL)
	{
		cout << "pd4 dynamic_cast failed" << endl << endl;
	}
	*/
}

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
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值