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
?
You could use
std::bind
to bind the member function.Or use a lambda.
BTW: It works well with const member function too, which doesn't matter here.
LIVE
One variant is just to use lambda:
Another variant is to use lambda and
std::mem_fn
(omitting the rest code of your class):And finally you may get rid of lambdas if you bind the object parameter of member function pointer:
All these also work with constant member functions.