0
回答
C++静态成员函数 调用非静态成员函数
终于搞明白,存储TCO原来是这样算的>>>   

        “海葵”真厉害,心情也很不爽。难道以后真的要做生产流水线上的IT民工么?

 

#include <iostream>

typedef void (__cdecl *CF)(void* _THIS, const void* _Param);

class A
{
public:
	A():m_A(5)
	{}

	void __cdecl say(const char* _str)
	{
		std::cout<<"Hello "<<_str<<std::endl;
		std::cout<<m_A<<std::endl;
	}
	
	static __stdcall void AA(void * _THIS, CF _fn,  void* _Param)
	{
		_fn(_THIS, (const char*)_Param);
	}

private:
	int m_A;
};

class B
{
public:
	B():m_B(55)
	{
	
	}

public:
      void __cdecl Show(const char* _str)
	  {
		  std::cout<<_str<<" "<<m_B<<std::endl;
	  }
private:
	int m_B;
};
template<class ADDRESS, class PFN>
void change(ADDRESS& _add, PFN _pFn)
{
	union
	{
		ADDRESS _newaddr;
		PFN     _newpFn;
	}t;

	t._newpFn   = _pFn;
	_add        = t._newaddr;

	return;
}

int main()
{
	
	A test;
	B test2;

	unsigned int THIS = reinterpret_cast<unsigned int>(&test);
	unsigned int address = -1;

	//通过第三方函数指针做中转站
	change(address, &A::say);
	CF a= reinterpret_cast<CF>(address);
	const char *str ="World";

	A::AA((void*)&test, a, (void*)(str));
	
	change(address, &B::Show);
	a= reinterpret_cast<CF>(address);
	A::AA((void*)&test2, a, (void*)(str));
	

	return 0;
}


 


原文链接:http://blog.csdn.net/template_cplus/article/details/7847774
<无标签>
举报
晨曦之光
发帖于5年前 0回/4K+阅
顶部