测试C++虚函数表

// 20171122_01.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "stdlib.h"
#include "iostream.h"
#include <stdio.h>

struct Base{
	int a;
	int b;

	virtual void Fn1()
	{
		printf("1111\n");
	}
	
	virtual void Fn2()
	{
		printf("2222\n");
	}
};

void TestMethod()
{
	Base base;
	printf("虚表的地址:%x \n",*((int*)(&base)));
	//定义函数指针
	typedef void(*pFunction)(void);
	pFunction pFn;
	int Temp = *((int*)(&base));
	pFn = (pFunction)(*((int*)(Temp)));
	pFn();
	pFn = (pFunction)(*((int*)(Temp)+1));
	pFn();
}

int main(int argc, char* argv[])
{


	TestMethod();

	return 0;
}

原文链接: 测试C++虚函数表 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-319.html )

发表评论

0则评论给“测试C++虚函数表”