The function template std::bind generate a forwarding call wrapper of origin function, it returns a function object.
We can just call the wrapper to invoke the origin function.
The class template std::function is function wrapper, the instance of it can be copied and store.

#include <iostream>

using namespace std;

class Base
{
public:
    Base( const int _number ){ number = _number; }
    void Print(){ cout << "Base: " << number << endl; }
private:
    int number;
};

void CoutNum( int _number )
{
    cout << "CoutNum: " << _number << endl;
}

int main()
{
    std::function<void(int)> fun0 = CoutNum;
    fun0( 10 ); // CoutNum: 10

    std::function<void()> fun1 = [](){ cout << "lambam expression" << endl; };
    fun1(); // lambam expression

    std::function<void()> fun2 = std::bind( CoutNum, 11 );
    fun2(); // CoutNum: 11

    fun2.swap( fun1 );
    fun2(); // lambam expression

    std::function<void(Base&)> fun3 = &Base::Print;
    Base obj( 110 );
    fun3( obj ); // Base: 110
    return 0;
}

Create call back binded with a class member function that has parameter. So we can use another class member function in the current class.

class MyWidget:public vtkContourWidget
{
//...
    typedef std::function<bool()> WidgetCheck;
    WidgetCheck   m_Check;

    void SetWidgetCheck(WidgetCheck ptr)
    {
        m_Check = ptr;
    }
}

bool CUModule::WidgetActionCheck(Type type)

auto checkCB = std::bind( &CUModule::WidgetActionCheck, this, type );
m_Widget->SetWidgetCheck( checkCB ); //m_Widget is the object of MyWidget
Categories: CPlusPlus

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x