输出成员函数地址小结

#include <iostream>
using namespace std;
class Ctest
{
public:
	static void statFunc()
	{
		cout << "statFunc" << endl;
	}
	void dynFunc()
	{  
		cout << "dynFunc" << endl;
	}
	virtual void virtFunc()
	{
		cout << "virtFunc" << endl; 
	}
};
int main()
{
	Ctest Object;

	cout << "address of Ctest::statFunc:" << &Ctest::statFunc << endl;
	cout << "address of Ctest::dynFunc :" << &Ctest::dynFunc << endl;
	cout << "address of Ctest::virtFunc:" << &Ctest::virtFunc << endl;

	static void (*p_statFunc)();
	void (Ctest::*p_dynFunc)();
	void (Ctest::*p_virtFunc)();
	p_statFunc = &Ctest::statFunc;
	p_dynFunc = &Ctest::dynFunc;
	p_virtFunc = &Ctest::virtFunc;
	p_statFunc();
	(Object.*p_dynFunc)();
	(Object.*p_virtFunc)();
	return 0;
}


用union逃避类型检查

#include <iostream>
using namespace std;

class A{
public:
	void fun()
	{
		cout << "Hi" << endl;
	}
};

template <class ToType, class FromType>
void GetMemberFuncAddr_VC6(ToType& addr,FromType f)
{
	union 
	{
		FromType _f;
		ToType   _t;
	}ut;

	ut._f = f;
	addr = ut._t;
}

int main()
{
	printf("00%X\n", &A::fun);
	void *x;
	GetMemberFuncAddr_VC6(x, &A::fun);
	cout << x << endl;
	return 0;
}


汇编

#include <iostream>
using namespace std;

#define GetMemberFuncAddr_VC8(FuncAddr, FuncType)	\
{													\
	__asm											\
	{												\
		mov eax,offset FuncType						\
	};												\
	__asm											\
	{												\
		mov FuncAddr, eax							\
	};												\
}


class A{
public:
	void fun()
	{
		cout << "Hi" << endl;
	}
};

template <class ToType, class FromType>
void GetMemberFuncAddr_VC6(ToType& addr,FromType f)
{
	union 
	{
		FromType _f;
		ToType   _t;
	}ut;

	ut._f = f;
	addr = ut._t;
}

int main()
{
	void *x;
	GetMemberFuncAddr_VC6(x, &A::fun);
	cout << x << endl;
	GetMemberFuncAddr_VC8(x, A::fun);
	cout << x << endl;
	return 0;
}

http://www.vckbase.com/index.php/wv/1514

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值