typeid 和虚函数

        上一篇C++类型预断——RTTI_master-计算机科学专栏-CSDN博客 讲到了typeid的动态类型推断需要借助虚函数表,本篇专门来讲一下这个原理,让大家理解的更透彻!

        前面咱们讲到 typeid 的操作返回值是 type_info 对象的引用,然后输出返回值的地址是相同的,测试代码如下:

#include <iostream>
#include <functional>
using namespace std;

class Base{
public:
    virtual 
	void test(){
		cout << "Base::test" << endl;
	}
};

class Derived : public Base{
public:
    void test(){
		cout << "Derived::test" << endl;
	}

	virtual 
	~Derived(){
		cout << "Derived::~Derived" << endl;
	}
};


int main()
{
	Base* pBase = new Base();
	Base* pBase2 = new Derived();
	Derived* pDerive = new Derived();
	
	//typeid(pBase2) 和 typeid(pDerive) 返回地址相同
    cout << "typeid(pBase2) = " << &typeid(*pBase2) <<  " typeid(pDerive) = "<< &typeid(*pDerive) << endl;

	
    return 0;
}

output信息:

typeid(pBase2) = 0x55dd724c6d48 typeid(pDerive) = 0x55dd724c6d48

        也就是说,0x55dd724c6d48 就是 Derived 类编译之后的类标识(type_info)数据信息!是否真的如此,咱们可以添加一下代码测试:

int main()
{
	Base* pBase = new Base();
	Base* pBase2 = new Derived();
	Derived* pDerive = new Derived();
	
	//typeid(pBase2) 和 typeid(pDerive) 返回地址相同
    cout << "typeid(pBase2) = " << &typeid(*pBase2) <<  " typeid(pDerive) = "<< &typeid(*pDerive) << endl;
	
	//class Base type_info 地址
	cout << "typeid(Base) = " << &typeid(Base)  << endl;

	//class Derive type_info 地址
	cout << "typeid(Derived) = " << &typeid(Derived)  << endl;

    //指针类型推导
    cout << "point ---- typeid(pBase2) = " << &typeid(pBase2) <<  " typeid(pDerive) = "<< &typeid(pDerive) << endl;

    return 0;
}

ouput信息:

typeid(pBase2) = 0x562309345d48 typeid(pDerive) = 0x562309345d48
typeid(Base) = 0x562309345d60
typeid(Derived) = 0x562309345d48

point ---- typeid(pBase2) = 0x562309345d28 typeid(pDerive) = 0x562309345d08

        可以看到,Derived 类的 type_info 信息的地址就是 0x558a4dec7d48 !要注意的一点:直接对指针类型进行操作,并不能返回正确的原始类型。

        好了嘛,那 typeid 到底是咋从虚函数表找到这个地址的呢?如果大家看过我之前的 深入理解new[]和delete[]_master-计算机科学专栏-CSDN博客 一文,应该就能够想到是不是C++编译器对虚函数表进行构造的过程中是不是也一样,做了地址偏移呢?

        咱们看看上面代码的汇编信息:

         通过查看汇编信息,我们得到以下结论:

  1. 虚函数表中确实存有typeinfo信息(第一个虚函数的地址偏移 -1 即是)
  2. typeinfo信息是区分指针类型是的(指针类型有前缀P,例如 P4Base、P7Derived)

        然后,我们仔细观察四个 typeinfo 类(Derived*、 Base*、Derived、Base),每个typeinfo 类都有一个虚函数表,继承自 vtable for __cxxabiv1::******* ,后面的信息会不一样。这里对该信息做一下简单说明:

  1. 对于启用了 RTTI 的类来说会继承 __cxxabiv1 里的某个类
  2. 所有的基础类(没有父类的类)都继承于_class_type_info
  3. 所有的基础类指针都继承自 __pointer_type_info
  4. 所有的单一继承类都继承自 __si_class_type_info
  5. 所有的多继承类都继承自 __vmi_class_type_info

以typeinfo for Derived为例:

然后是指向存储类型名字的指针,
如果有继承关系,则最后是指向父类的 typeinfo 的记录。

         所以,如果是正常调 typeinfo 基类(_class_type_info、__pointer_type_info、__si_class_type_info、__vmi_class_type_info)的方法应该会动态调到 type_info 的继承类 (typeinfo for Derived*、typeinfo for  Base*、typeinfo for Derived、typeinfo for Base)的方法。

        但是,typeid 操作指针类型时并不是这样,说明C++编译器底层有特殊处理!

调试以下代码:

    cout << typeid(*pBase2).name();
    cout << typeid(*pDerive).name();

    cout << typeid(pBase2).name();
    cout << typeid(pDerive).name();

        通过汇编信息,可以看到这里并没有做任何动态调用的逻辑,而是直接返回该指针类型的typeinfo信息,这也就解释了为什么 typeid 操作指针和操作对象的结果不一样!

         那么我们在使用typeid时,如果要获取到真实对象类型,应该要将指针去掉!

        为了验证我们前面的结论: 虚函数表中确实存有typeinfo信息(第一个虚函数的地址偏移 -1 即是)咱们可以直接通过指针的方式操作虚函数表!

        测试代码如下:

#include <iostream>
#include <functional>
using namespace std;

class Base{
public:
    virtual 
	void test(){
		cout << "Base::test" << endl;
	}
};

class Derived : public Base{
public:
    void test(){
		cout << "Derived::test" << endl;
	}

	virtual 
	~Derived(){
		cout << "Derived::~Derived" << endl;
	}
};


typedef void (*FUNPTR)();

type_info* getTypeInfo(unsigned long ** vtbl){
	type_info* typeinfo = (type_info*)((unsigned long)vtbl[-1]);
	return typeinfo;
}

void visitVtbl(unsigned long ** vtbl, int count)
{
    cout << vtbl << endl;
    cout << "\t[-1]: " << (unsigned long)vtbl[-1] << endl;
 
    typedef void (*FUNPTR)();
    for (int i = 0; vtbl[i] && i < count; ++i)
    {
        cout << "\t[" << i << "]: " << vtbl[i] << " -> ";
        FUNPTR func = (FUNPTR)vtbl[i];
        func();
    }
}
int main()
{
	Base* pBase = new Base();
	Base* pBase2 = new Derived();
	Derived* pDerive = new Derived();
	
	//这里去遍历虚函数表
	visitVtbl((unsigned long **)*(unsigned long **)pBase2, 2);

	//获取虚函数表-1位置的typeinfo地址
	cout << "pDerive = " << getTypeInfo((unsigned long **)*(unsigned long **)pDerive) << " "
	<< getTypeInfo((unsigned long **)*(unsigned long **)pDerive)->name() << endl;
	
	//获取虚函数表-1位置的typeinfo地址
	cout << "pBase2 = " << getTypeInfo((unsigned long **)*(unsigned long **)pBase2) << " "
	<< getTypeInfo((unsigned long **)*(unsigned long **)pBase2)->name() << endl;


    return 0;
}

        这里要注意的一点是,遍历虚函数表 visitVtbl 方法的第2个参数,自己控制不要越界,此外,还要注意调用的顺序,如果先调用了虚析构函数,会导致内存错误!

output信息:

0x5620022edd10
        [-1]: 94695475567936
        [0]: 0x5620022eb5fa -> Derived::test
        [1]: 0x5620022eb636 -> Derived::~Derived
pDerive = 0x5620022edd40 7Derived
pBase2 = 0x5620022edd40 7Derived

         通过直接访问虚函数表-1位置,我们可以看到输出的日志信息与我们前面的结论是一致的!也即是C++编译器给我们做了偏移操作(在-1的位置存储了type_info信息,实例化对象中的虚函数表地址是偏移之后的地址)。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chls

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值