boost::bind 函数与类成员的区别

class CP

{

public:

    int print(int a, int b)

    {

        printf("CP print() a=%d, b=%d");

    }

};

 

int print(int a, int b)

{

    printf("print() a=%d, b=%d");

}

 

CP f &rf=f;

CP* pf;

bind(print, _1, _2)(10, 20);                      //ok

bind(&print, _1, _2)(10, 20);                   //ok

bind(CP::print, f, _1, _2)(10, 20);             //error     

bind(&CP::print, f, _1, _2)(10, 20);           //ok

bind(&CP::print, rf, _1, _2)(10, 20);           //ok

bind(&CP::print, pf, _1, _2)(10, 20);           //ok

 

error原因:

CP::print必需要&  表明这是一个成员函数指针

 bind也可以绑定类的成员,用法同绑定成员函数,加&;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章