总结有以下几个办法:
1.使用类的静态成员:
最简单的.只是这种办法与使用普通函数没有什么区别
2.模板的方法
1
template
<
class
T
>
2 void Call(T * ptr, void (T:: * MenFn)())
3 {
4 (ptr ->* MenFn)();
5 }
2 void Call(T * ptr, void (T:: * MenFn)())
3 {
4 (ptr ->* MenFn)();
5 }
1
class
MyClass
2 {
3 public :
4 void Call()
5 {
6
7 }
8 };
9
10 MyClass cs;
11 Call( & cs, & MyCall::Call);
2 {
3 public :
4 void Call()
5 {
6
7 }
8 };
9
10 MyClass cs;
11 Call( & cs, & MyCall::Call);
3. 虚拟继承的方法
1
class
Base
2 {
3 public :
4 virtual void Call() = 0 ;
5 };
6
7 class SubClass : public Base
8 {
9 public :
10 void Call(){}
11 };
12
13 void Call(Base * ptr)
14 {
15 ptr -> Call();
16 }
2 {
3 public :
4 virtual void Call() = 0 ;
5 };
6
7 class SubClass : public Base
8 {
9 public :
10 void Call(){}
11 };
12
13 void Call(Base * ptr)
14 {
15 ptr -> Call();
16 }
4.use boost.
1
#include
<
boost
/
bind.hpp
>
2 #include < iostream >
3 using namespace std;
4 struct MyClass
5 {
6 void Print(const char* str)
7 {
8 cout<<"holle "<<str<<endl;
9 }
10} ;
11
12 template < class _Fun >
13 void CallBackFun(_Fun a, const char * str)
14 {
15 t(n);
16}
17
18 int main()
19 {
20 MyClass mc;
21 CallBackFun(boost::bind(boost::mem_fn(&MyClass::Print),&mc,_1),"world")
22 return 1;
23}
24
2 #include < iostream >
3 using namespace std;
4 struct MyClass
5 {
6 void Print(const char* str)
7 {
8 cout<<"holle "<<str<<endl;
9 }
10} ;
11
12 template < class _Fun >
13 void CallBackFun(_Fun a, const char * str)
14 {
15 t(n);
16}
17
18 int main()
19 {
20 MyClass mc;
21 CallBackFun(boost::bind(boost::mem_fn(&MyClass::Print),&mc,_1),"world")
22 return 1;
23}
24
5.使用静态成员函数或者联合体模拟之
1
union
2 {
3 void (G_CALL ParticleSystem:: * PhysicsThreadFun)( void * arg);
4 ThreadFun f;
5 }fn;
6 fn.PhysicsThreadFun = & ParticleSystem::PhysicsThreadFun;
7 thread_id = CreateThread(fn.f, & ps);
2 {
3 void (G_CALL ParticleSystem:: * PhysicsThreadFun)( void * arg);
4 ThreadFun f;
5 }fn;
6 fn.PhysicsThreadFun = & ParticleSystem::PhysicsThreadFun;
7 thread_id = CreateThread(fn.f, & ps);