(C++对象模型):RTTI运行时类型识别回顾与存储位置介绍

RTTI(运行时类型识别)简单回顾

class Base
{
public:
	virtual void f() { cout << "Base::f()" << endl; }
	virtual void g() { cout << "Base::g()" << endl; }
	virtual void h() { cout << "Base::h()" << endl; }	
	virtual ~Base() {}	
};

class Derive :public Base {
public:	
	virtual void g() { cout << "Derive::g()" << endl; }
	void myselffunc() {} //只属于Derive的函数
	virtual ~Derive() {}
};

int main(){
	Base *pb = new Derive(); 
	pb->g();

	Derive myderive;
	Base &yb = myderive;
	yb.g();

	return 1;
}
  • 若在上述main函数中的尾部加入如下代码:
...
	cout << typeid(*pb).name() << endl;
	cout << typeid(yb).name() << endl;
...
  • 输出

  • 运行时类型识别RTTI,要求父类中必须至少有一个虚函数;如果父类中没有虚函数,那么得到RTTI就不准确
  • RTTI就可以在执行期间查询一个多态指针,或者多态引用的信息了
  • RTTI能力靠typeiddynamic_cast运算符来体现
...	
    Derive *pderive = dynamic_cast<Derive *>(pb);
	if (pderive != NULL)
	{
		cout << "pb实际是一个Derive类型" << endl;
		pderive->myselffunc(); //调用自己专属函数
	}
...

RTTI实现原理

  • typeid返回的是一个常量对象的引用,这个常量对象的类型一般是type_info(类)
...

	const std::type_info &tp = typeid(*pb);
	Base *pb2 = new Derive();
	Base *pb3 = new Derive();
	const std::type_info &tp2 = typeid(*pb2);
	const std::type_info &tp3 = typeid(*pb3);



	cout << typeid(int).name();

	if (tp == tp2)
	{
		cout << "很好,类型相同" << endl;
	}

	//其他用法,静态类型;不属于多态类型
	cout << typeid(int).name() << endl;
	cout << typeid(Base).name() << endl;
	cout << typeid(Derive).name() << endl;
	Derive *pa3 = new Derive();
	cout << typeid(pa3).name();

...

  • RTTI的测试能力跟基类中是否有虚函数表有关系,如果基类中没有虚函数,也就不存在基类的虚函数表,RTTI就无法得到正确结果
Base *pb = new Derive();
Derive myderive;
Base &yb = myderive;
cout << typeid(*pb).name() << endl; //class Derive
cout << typeid(yb).name() << endl; //class Derive
Base *pb2 = new Derive();
const std::type_info &tp2 = typeid(*pb2);
  • typid对于有虚函数的对象的反汇编


...
	Base *pb2 = new Derive();
	const std::type_info &tp2 = typeid(*pb2);
	printf("tp2地址为:%p\n", &tp2);
	long *pvptr = (long *)pb2;
	long *vptr = (long *)(*pvptr);
	printf("虚函数表首地址为:%p\n", vptr);
	printf("虚函数表首地址之前一个地址为:%p\n", vptr-1); //这里的-1实际上是往上走了4个字节

	long *prttiinfo = (long *)(*(vptr - 1));
	prttiinfo += 3; //跳过12字节
	long * ptypeinfoaddr = (long *)(*prttiinfo);
	const std::type_info *ptypeinfoaddrreal = (const std::type_info *)ptypeinfoaddr;
	printf("ptypeinfoaddrreal地址为:%p\n", ptypeinfoaddrreal);
	cout << ptypeinfoaddrreal->name() << endl; 
...
  • 调用图示

vptr,vtbl,rtti的type_info信息 构造时机

  • rtti的type_info信息:编译后就存在了,写到了可执行文件中
  • 总结:各个编译器实现有一定差异,但总体都是以虚函数表开始地址为突破口。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值