Let's say that I have a class with three member functions, as follows:
#include <iostream>
#include <functional>
class ClassName
{
public:
double add(double a, double b);
double intermediate(double a, double b, std::function<double (double,double)> func);
double combiner(double a, double b);
};
double ClassName::add(double a, double b)
{
return a+b;
}
double ClassName::intermediate(double a, double b, std::function<double (double,double)> func)
{
return func(a, b);
}
double ClassName::combiner(double a, double b)
{
return intermediate(a, b, add);
}
int main()
{
ClassName OBJ;
std::cout << OBJ.combiner(12, 10);
}
What I'm wanting to do is pass the member function "add" to the member function "intermediate" which is then called by "combiner." However, I don't think that I'm using the right syntax, because when I attempt to compile this, I get an error saying "non-standard syntax; use '&' to create a pointer to member." I'm not quite sure what's going wrong, because this method works perfectly if these functions are not member functions in a class (just regular functions defined in the namespace). So is it possible to pass a member function to another member function?