a concerned citizen a concerned citizen - 3 months ago 11
C++ Question

C++ External function with pointer to function as parameter, used inside a class with a member function

Fairly new to C++. Suppose I have a class:

class A
{
private:
double m_x, m_y;
public:
A(double x, double y): m_x {x}
{
m_y = extF(m_x, y, *intF);
}

double intF(double x) { return 2*x; }
};


And it makes use of an external global function, defined elsewhere:

double extF(double x, double y, std::function<double(double)> f)
{
if (x*y < 0)
return f(x);
else
return f(y);
}


Formulas are bogus. This does not compile. I tried simple
intF
,
A::*intF
,
&A::intF
, even some unorthodox combinations, but that's just guessing. The problem is that class
A
is not the only one which makes use of the global external function and it's something that should be able to be a user choice at runtime. Searches revealed some answers saying it's not possible to make a pointer to a member function like this because it needs instantiation(?), but I found no solutions. Can this be done? If yes, how?




Edit: Additional question: how can the pointer to member function be done if the member function is
const double f(...) const
?

Answer

One variant is just to use lambda:

class A
{
private:
    double m_x, m_y;
public:
    A(double x, double y): m_x {x}
    {
         m_y = extF(m_x, y, [&](double d){ return intF(d);});
    }

    double intF(double x) { return 2*x; }
};

Another variant is to use lambda and std::mem_fn (omitting the rest code of your class):

A(double x, double y): m_x {x}
{
    auto fn = std::mem_fn(&A::intF);
    m_y = extF(m_x, y, [&](double d) {return fn(this, d);});
}

And finally you may get rid of lambdas if you bind the object parameter of member function pointer:

A(double x, double y): m_x {x}
{
    auto fn1 = std::bind(std::mem_fn(&A::intF), this, std::placeholders::_1);
    m_y = extF(m_x, y, fn1);
}

All these also work with constant member functions.

Comments