some ways to call member functions

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

  return 0;
}

Leave a Reply

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