C++ 虚函数表

#include <bits/stdc++.h>
using namespace std;

class Base{
public:
	virtual void f() {
		cout << "Base::f" << endl;
	}
	virtual void g() {
		cout << "Base::g" << endl;
	}
	virtual void h() {
		cout << "Base::h" << endl;
	}
};
class Sub1 : public Base{
public :
	virtual void f1(){
		cout << "Sub1::f1" << endl;
	}
	virtual void g1(){
		cout << "Sub1::g1" << endl;
	}
	virtual void h1(){
		cout << "Sub1::h1" << endl;
	}
};

typedef void(*Fun)();
int main(){
	Sub1 b;

	Fun pFun = NULL;
	int* p = (int*)&b;
	((Fun)*((int*)*(int*)p))();
	((Fun)*((int*)*(int*)p + 1))();
	((Fun)*((int*)*(int*)p + 2))();
	((Fun)*((int*)*(int*)p + 3))();
	((Fun)*((int*)*(int*)p + 4))();
	((Fun)*((int*)*(int*)p + 5))();
	/*
	输出为
		Base::f
		Base::g
		Base::h
		Sub1::f1
		Sub1::g1
		Sub1::h1
	*/
}

posted on 2016-04-16 19:29  张济  阅读(135)  评论(0编辑  收藏  举报

导航