I have read several posts about this, but can't seem to find exactly what i am looking for with example code if anyone could give me some help i would highly appreciate it.
in my header i have:
void addEvent(void (*func)(Pack *));
void triggerEvents(Pack * );
std::list<void(*)(Pack *)> eventList;
and in cpp file
void DNetwork::addEvent(void (*func)(Pack *)){
eventList.push_back(func);
}
void DNetwork::triggerEvents(Pack * pack){
for (std::list<void (*)( Pack *)>::iterator it = eventList.begin(); it != eventList.end() ;it++ ){
(*it)(pack);
}
}
This works fine with free functions, but when i try to add a member function to this list i get an error. Does anyone know how to store a member function (from random class objects) inside a pointer?
For member function you need a bind. A member function is a "normal function" that has an implicit parameter of its class. So you need a binder. If you use c++11 you can use std::bind and std::function or you can use boost::bind and boost::function for non c++11 code.
Now if I have the class A with the method
doA( Pack* )
I will write:Or even better you can use Boost.Signal or you can use the Publisher/Subcriber Pattern
Edit As @DavidRodríguez-dribeas suggest: The bind should not take the &pack argument, as the argument to the member function is provided at the place of call in triggerEvents. The correct way is:
If I'm understanding your problem you get the error when you try to add a function to the list in addEvent?
If you're adding a pointer to a non-static member function of a class ensure it has the right syntax... for example a function pointer to a a member function in TestClass would look like:
You could do an overload such as:
The simple solution is using type erasure on the function/function pointer type, for which the easier way is just using
std::function<>
:Then you can initialize the
function
objects with either a free function or a member function (by means ofstd::bind
to bind the member-function with an object on which to call it) or even function objects (types that offer anoperator()
).