虚函数表测试2

// 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{

	virtual void Fn1()
	{
		printf("Base:Fn1111\n");
	}
	
	virtual void Fn2()
	{
		printf("Base:Fn22222\n");
	}

	virtual void Fn3()
	{
		printf("Base:Fn33333\n");
	}

};


struct Sub:Base{
	
	virtual void Fn4()
	{
		printf("Base:Fn4444\n");
	}
	
	virtual void Fn5()
	{
		printf("Base:Fn55555\n");
	}
	
	virtual void Fn6()
	{
		printf("Base:Fn66666\n");
	}
	
};

void TestMethod()
{
	Sub mySub;
	printf("虚表的地址:%x \n",*((int*)(&mySub)));
	//定义函数指针
	
	typedef void(*pFunction)(void);
	pFunction pFn;

	//打印所有的虚函数表
	int Temp = *((int*)(&mySub));
	for(int i=0;i<6;i++)
	{
		int Fn = *((int*)Temp+i);
		printf("%d -- %x \n",i,Fn);
		pFn = (pFunction)Fn;
		pFn();

	}

}

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


	TestMethod();

	return 0;
}

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

发表评论

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