Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Member Function as Parameter to other Member Function (C++ 11 <function>)

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?

like image 912
JohnTravolski Avatar asked Jun 28 '17 00:06

JohnTravolski


People also ask

How do you pass a function as a parameter to another function?

You cannot pass function to another function as parameter. But, you can pass function reference to another function using function pointers. Using function pointer you can store reference of a function and can pass it to another function as normal pointer variable.

How do you call a member function from another member function in C++?

just make simple call like below................ class abc{ member_func1(); member_func2(); }; abc::member_func1(){ //..........................

Can we pass a function to another function in C?

A function can also be passed to another function by passing its address to that function; In simple terms, it could be achieved via pointers. Example: C++

Can we pass function as parameter?

We cannot pass the function as an argument to another function. But we can pass the reference of a function as a parameter by using a function pointer. This process is known as call by reference as the function parameter is passed as a pointer that holds the address of arguments.


2 Answers

ClassName::add is a non-static member function, an instance of ClassName is needed for it to be called on; it can't be used as the argument for std::function<double (double,double)> directly.

You can use lambda and capture this (as @Igor Tandetnik commented):

return intermediate(a, b, [this](double x, double y) { return add(x, y); } );

or use std::bind and bind this pointer:

return intermediate(a, b, std::bind(&ClassName::add, this, _1, _2));

or make ClassName::add a static member function or a non-member function (it could be because it doesn't use any members of ClassName). e.g.

class ClassName
{
  public:
    static double add(double a, double b);
    ...
};
like image 155
songyuanyao Avatar answered Oct 07 '22 11:10

songyuanyao


If you truly want to pass the member function, you need a member function pointer

class ClassName
{
   public:
    double add(double a, double b);
    using Combiner = double (ClassName::*)(double, double);
    double intermediate(double a, double b, Combiner);
    double combiner(double a, double b);
};

This will only slightly change the implementation of intermediate and combiner

double ClassName::intermediate(double a, double b, Combiner func)
{
  return (this->*func)(a, b);
}

double ClassName::combiner(double a, double b)
{
  return intermediate(a, b, &ClassName::add);
}
like image 45
HeroicKatora Avatar answered Oct 07 '22 11:10

HeroicKatora