Function Pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Function Pointer

Can any one explain why this is not working please!!! https://code.sololearn.com/cC4d9h1a77jv

14th Sep 2021, 7:09 AM
Eyob
Eyob - avatar
1 Answer
+ 4
In order to call a wrapped member function from std::function<>, you need to pass it the object to call the method from as first argument, since unlike free functions, a member function can only be called from a class instance. You can either change the signature of caller() to accomodate for that additional argument, or bind it to the member function when creating the function object in main(). Here is an example for the latter: maker->caller( std::bind( &Other::setData, oth, std::placeholders::_1 ) ); Note the placeholder, which is used to specify an unbound argument, so that the string parameter can still be passed into it when actually calling the function. For reference and some additional examples, you can refer to: https://en.cppreference.com/w/cpp/utility/functional
14th Sep 2021, 8:35 AM
Shadow
Shadow - avatar