本文介绍了vtable和虚拟指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好,

我想了解vtable&vptr实现的内部原理,我有一些疑问.如果有人可以详细了解所有这些内容.

1:何时创建vtable和vptr?是编译时还是运行时?
2:如何打印vptr的地址和vptr的包含内容,即vtable的地址?是否可以打印这些地址.如果没有,那为什么呢?

等待快速回复.............

Hi All,

I am trying to understand the internals of vtable & vptr implementation and I have some question. If anybody can explore in details about all these.

1: When does vtable and vptr getting created? Is it compile time or at Run time?
2: how can we print the address of vptr and containt of vptr, i.e. the address of vtable? Is it possible to print these addresses. if no then why?

Waiting for quick reply .............

推荐答案


aobj *temp = new aobj(); //NOTE MUST CONTAIN NON PURE VIRTUAL FUNCTION

int* vptr =  *(int**)temp; //get the  vptr

//assmebly code to make "temp" as the "this" ptr
__asm
{
  mov ecx, temp
}

 ((void (*)()) vptr[0] )(); //this will call the first function in vtable


这篇关于vtable和虚拟指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 12:37