mardi 27 octobre 2015

Parameterizing a function using a struct

The code below does not compile. The function foo takes a function pointer f as argument, and f takes 1 int as argument and returns an int, however, I'd like to add a parameter to f.

#include <iostream>

void foo(int(*f)(int))
{
  std::cout << f(3) << std::endl;
}

struct plusX
{
  plusX(int x_) : x(x_) {}
  int x;
  int plus(int y)
  {
    return x + y;
  }
};

int main()
{
  plusX px4(4);
  a(&px4.plus);  // ERROR!
}

ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say '&plusX::plus'

Aucun commentaire:

Enregistrer un commentaire