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也可以綁定類的成員,用法同綁定成員函數,加&;

 

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