将指向一个类的成员函数的函数的指针转换为另一个类的成员函数

时间:2019-05-06 10:37:23

标签: c++11

我已经通过使用std :: bind和函数对象成功实现了。但是我想使用函数指针。 我无法使用函数指针,在注册回叫时遇到问题。

class A{
  public:
    int id;
    A(){};
    typedef void(*fptr_t)(void); // function pointer
    fptr_t f1=NULL;
    void operator()(int i)  //using functors  
    {
      id=i;
      cout<<"Object number "<<id<<endl;
    }

    void call_bk_handler(fptr_t fp)
    {
      f1= fp;
    }

.........

};


class B {
public:
  B(int N):baseObj(N),m_total_clicks(0),m_clicks(N,0)
  {
     for(int i=0;i<N;i++)
     {
        A[i](i); // using functors;
        A[i].call_bk_handler(&B::callBack); // this is not working
     }
  void callBack(){
  .............
  };

}

std::vector<A>basedObj;
public:
int m_total_clicks;

std::vector<int>m_clicks
int count=0;

0 个答案:

没有答案