AppleDemo.h:
class CAppleDemo
{
public:
CAppleDemo();
~CAppleDemo();
void fun1(void);
void fun2(int a, int b);
int fun3(int a);
};
AppleDemo.cpp
#include "stdafx.h"
#include "AppleDemo.h"
#include <iostream>
CAppleDemo::CAppleDemo()
{
std::cout << "CAppleDemo::CAppleDemo()" << std::endl;
}
CAppleDemo::~CAppleDemo()
{
std::cout << "CAppleDemo::~CAppleDemo()" << std::endl;
}
void CAppleDemo::fun1(void)
{
std::cout << "CAppleDemo::fun1()" << std::endl;
}
void CAppleDemo::fun2(int a, int b)
{
std::cout << "CAppleDemo::fun2() a=" << a << " b=" << b << std::endl;
}
int CAppleDemo::fun3(int a)
{
std::cout << "CAppleDemo::fun3() a=" << a << std::endl;
return a;
}
main.cpp
#include "stdafx.h"
#include<stdio.h>
#include <stdlib.h>
#include "AppleDemo.h"
//javascript:void(0)
template<typename dst_type, typename src_type>
dst_type pointer_cast(src_type src)
{
return *static_cast<dst_type*>(static_cast<void*>(&src));
}
template<typename dst_type, typename src_type>
dst_type union_cast(src_type src)
{
union{
src_type s;
dst_type d;
}u;
u.s = src;
return u.d;
}
typedef void (__fastcall *FunFun1)(void* pThis, int edx);
typedef void (__fastcall *FunFun2)(void* pThis, int edx, int a, int b);
typedef int (__fastcall *FunFun3)(void* pThis, int edx, int a);
int _tmain(int argc, _TCHAR* argv[])
{
CAppleDemo cAppleDemo;
FunFun1 fun1 = pointer_cast<FunFun1>(&CAppleDemo::fun1);
FunFun2 fun2 = pointer_cast<FunFun2>(&CAppleDemo::fun2);
FunFun3 fun3 = union_cast<FunFun3>(&CAppleDemo::fun3);
fun1(NULL, NULL);
fun2(NULL, NULL, 11, 22);
fun3(NULL, NULL, 33);
//cAppleDemo.fun1();
//cAppleDemo.fun2(11, 22);
//cAppleDemo.fun3(33);
getchar();
return 0;
}
效果如下: