some ways to call member functions

#include <iostream>
#include <functional>
 
using namespace std;
 
struct bar {
  void f() { cout << "hi"; }
};
 
void foo1(bar &amp;c, void (bar::*function)()) { (c.*function)(); }
template <typename Handler> void foo2(bar &amp;c, Handler h) { h(c); }
template <typename T> void foo3(T fun) { fun(); }
 
int main() {
  bar b;
  foo1(b, &amp;bar::f);
  foo2(b, std::mem_fn(&amp;bar::f));
  foo3([&amp;b]() { b.f(); });

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *