jeudi 2 juillet 2015

Reference to class method

I would like to pass to a function a reference to a class method.

For example:

#include <functional>

struct Foo
{
  int f(const int& val) const
  {
    return val+2;
  }
};

int caller(const std::function<int(const int&)>& f)
{
  return f(1);
}

int main()
{
  caller([](const int& val){return val+2;}); // OK
  Foo foo;
  caller(foo.f); // WRONG
  return 0;
}

How can I fix the second call of caller() (NB: Foo:f() is not static)?

Aucun commentaire:

Enregistrer un commentaire