-2

I am working on c++ 11.

I want to write a function funA in a class, which binds another function funB within the same class. And funB function is a parameter in the function funA. What could be the syntax of the funcA ? I tried using std::function func, but i am not able to compile. Please explain.

Thanks.

class ServiceClass
{
typedef std::function<void(const int&, const int&)> ServiceCallBack;

public:
void ServiceFunc(ServiceCallBack callback)
{
    callback(1,2);
}
};

class MyClass
{
public:

ServiceClass serviceClass;

void myFunction(int i)
{
    cout << __FUNCTION__ << endl;
    cout << i << endl;
}

void myFunction2(int i)
{
    cout << __FUNCTION__ << endl << i << endl;
}

void bindFunction(std::function<void()> func)
{
    std::bind(func, this, std::placeholders::_1);
    func();
}

void testmyFunction()
{
    serviceClass.ServiceFunc( std::bind(
                                  MyClass::myFunction,
                                  this,
                                  std::placeholders::_1
                                  ));
}

void testmyFunction2()
{
    serviceClass.ServiceFunc( std::bind(
                                  MyClass::myFunction2,
                                  this,
                                  std::placeholders::_1
                                  ));
}

void testFunctions( int i )
{
    if( i == 1 )
    {
        serviceClass.ServiceFunc( std::bind( MyClass::myFunction, this, std::placeholders::_1 ));
    }
    else if( i == 2 )
    {
        serviceClass.ServiceFunc( std::bind( MyClass::myFunction2, this, std::placeholders::_1 ));
    }
}
};

Based on some condition, in the function testFunctions, i want to call any of the callback functions, myFunction or myFunction2. So if a can modify the testFunctions to receive a parameter which can take any of the callback functions, then I dont have to write the if else conditions.

Please suggest.

SuryaRao
  • 51
  • 1
  • 6
  • 1
    Provide a snippet please. – Hatted Rooster Aug 29 '18 at 19:19
  • 3
    There is a sample program on [std::bind](https://en.cppreference.com/w/cpp/utility/functional/bind). I think you should read and try that first. If something is still not clear after that, just post your doubt with some sample code. – badola Aug 29 '18 at 19:22
  • I edited that into the question for you. Can you edit your question to include more details about the problem? Are you asking how to use `std::bind` with `&MyClass::myFunction`? – Barry Aug 29 '18 at 19:30
  • Yes, my question is the function bindFunction can take any parameter (function of the same type) which can be binded. what is the correct syntax of the parameter of the function bindFunction ? – SuryaRao Aug 29 '18 at 19:32
  • Possible dupe: https://stackoverflow.com/q/17131768/2069064 – Barry Aug 29 '18 at 19:35
  • Just a suggestion: use a lambda instead. They can do all that `std::bind` does and more + they are more readable (IMO). – Jesper Juhl Aug 29 '18 at 19:38
  • Can function pointers be used instead here ? – SuryaRao Aug 29 '18 at 19:40
  • Let's step back from implementation details - what is the point of the exercise? What should `bindFunction` do, exactly? What's the expected outcome of `bindFunction(&MyClass::myFunction)` call? – Igor Tandetnik Aug 30 '18 at 01:02
  • I will try to explain with an example. – SuryaRao Aug 30 '18 at 02:58
  • @ Igor Tandetnik I have modified the question accordingly. please check – SuryaRao Aug 30 '18 at 03:07
  • I guess I found what I was looking for, by using function pointers in this way. `typedef void(MyClass::*MY_CLASS_PTR)(int); void testFunctions( MY_CLASS_PTR fptr) { serviceClass.ServiceFunc( std::bind( fptr , this, std::placeholders::_1 )); } // passing myFunction as a parameter to be used for std::bind testFunctions(&MyClass::myFunction); – SuryaRao Sep 01 '18 at 19:06

1 Answers1

0
typedef void(MyClass::*MY_CLASS_PTR)(int);
// function which takes function pointer as parameter
void testFunctions( MY_CLASS_PTR fptr)
{
// fptr should have which function to bind 
  serviceClass.ServiceFunc( std::bind( fptr , this, std::placeholders::_1 ));
}
SuryaRao
  • 51
  • 1
  • 6
  • 2
    Simply posting some code isn't terribly helpful. Can you explain your code? That way others can understand and learn from your answer instead of just copying & pasting some code from the web. – Robert Sep 01 '18 at 21:23
  • The format of the function which takes a function pointer which can be used to bind that function pointer was the question. typedef void(MyClass::*MY_CLASS_PTR)(int); We have to give the name of the class while defining a function pointer which points to functions within the class. This was missing when I tried. So the function could look like this. void testFunctions( MY_CLASS_PTR fptr) @Robert : The answer was not juts copied from any website. It was part of the sample program. Referred to https://en.wikipedia.org/wiki/Function_pointer – SuryaRao Sep 02 '18 at 06:22