• 输出成员函数地址小结


    #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
    ", &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

    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    AE计算Tin的体积
    Create a Geometric Network and Set Flow Direction
    为什么查询的时候只能获取FID字段
    美国白蛾入侵北京 GIS兵法破解危局
    字段排序
    如何对ArcSDE空间网格大小进行优化?
    How to create a geodatabase network dataset
    抗击海冰 地理信息系统来帮忙
    控件图片生成网站
    Pascal 之(冒泡排序)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834862.html
Copyright © 2020-2023  润新知