Search code examples
functionc++11stdbind

C++ is it possible to bind multiple methods together?


I would like to call a method after another method using std::function. Suppose i have something like this

class foo
{

   std::function<void(int)> fptr;

   void bar(int){
   }
   void rock(){
   }
public:

   foo() {
       fptr = bind(&foo::bar,this,std::place_holder::_1); //<------Statement  1
   }
}

Now because of statement 1 if i call fptr(12) the method bar is called. My question is can i specify or manipulate statement 1 so that after bar is called it calles rock. I know i could simply have bar calle rock but thats not what i am looking for. Can bind help me accomplish this ?


Solution

  • std::bind won't really help here, but a lambda will.

    fptr = [this](int n) { bar(n); rock(); };