c++中使用成员函数作为函数参数的方法

总结有以下几个办法:
1.使用类的静态成员:
      最简单的.只是这种办法与使用普通函数没有什么区别
2.模板的方法
 

1    template < class   T >    
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);

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  } 

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

  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);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值