I have searched for this, but i think i am just making myself more confused.
What I am trying to do save a function pointer in an object and have it be called later in another thread.
What i have been envisioning is a constructor that would take a function pointer and the parameters that will be passed to this function pointer. This object will also have a run() method which will run the said function pointer and a wait_until_completed() method that block until the function has been ran.
The function pointer should is going to be a function from another object if that makes any sense. For example
Foo::*Bar(int);
I have the wait_until_completed() working using pthread_cond_t, but am stuck on this function pointer thing and feel like i am just running around in circles.
Any advice?
EDIT: This is for school (any my general understanding) so third party libraries wont work :/
I felt like i did a very poor job explaining this, to let me give some sample code (excluding all the synchronizing stuff)
class Foo
{
public:
Foo(void (Bar::*function) (int), int function_param)
{
// Save the function pointer into this object
this->function = &function;
// Save the paramater to be passed to the function pointer in this object
param = function_param;
}
run()
{
(*function)(param);
}
private:
// The function pointer
void (Bar::*function) (int) = NULL;
// The paramater to pass the function pointer
int param;
}
This is in a nutshell what i am trying to do. However, im not sure if it is a syntax thing or me being stupid, but i cannot figure out how to actually do this and get it to compile.
Thoughs? And thanks for all the advice so far :)
Function pointers can be stored in variables, structs, unions, and arrays and passed to and from functions just like any other pointer type. They can also be called: a variable of type function pointer can be used in place of a function name.
A pointer to a function points to the address of the executable code of the function. You can use pointers to call functions and to pass functions as arguments to other functions. You cannot perform pointer arithmetic on pointers to functions.
Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.
Some people feel you should not use the this pointer in a constructor because the object is not fully formed yet. However you can use this in the constructor (in the {body} and even in the initialization list) if you are careful.
First, you would want to typedef
your function type, making it easier to reuse it and lessen the potential of mistakes.
typedef void (Bar::*function_type)(int);
Now you can use the typedef like this:
Foo(function_type func, int func_param)
: function_(func)
, param_(func_param)
{
}
Also, it's a good idea to use the initialization list to initialize member variables (you can find more about initialization lists here).
However, you would still not be able to call the function. Class member functions, also called bound functions MUST be used with an instantiated object, because they are bound to them. Your run
function would have to look like this (you also forgot the return type):
void run(Bar* object){
(object->*function_(param_));
}
It uses the special ->*
operator to call member functions via a member function pointer.
Also, you can't initialize most variables directly inside the class (only static integrals constants like a static const int i = 5;
).
Now you would be able to instantiate a Foo
object and call the run function on it.
Here you got a fully compileable example:
#include <iostream>
using namespace std;
class Bar{
public:
void MyBarFunction(int i){
cout << i << endl;
}
};
class Foo
{
public:
typedef void (Bar::*function_type)(int);
Foo(Bar* object, function_type function, int function_param)
: object_(object) // save the object on which to call the function later on
, function_(function) // save the function pointer
, param_(function_param) // save the parameter passed at the function
{
}
void Run()
{
(object_->*function_)(param_);
}
private:
// The object to call the function on
Bar* object_;
// The function pointer
function_type function_;
// The paramater to pass the function pointer
int param_;
};
int main(void){
// create a Bar object
Bar bar;
// create a Foo object, passing the Bar object
// and the Bar::myBarFunction as a parameter
Foo foo(&bar, &Bar::MyBarFunction, 5);
// call Bar::MyBarFunction on the previously passed Bar object 'bar'
foo.Run();
cin.get();
return 0;
}
This may be a bit much to digest at once, but I hope this helps you understanding member function pointers and how to use them. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With