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.
typedef std::function< void ( Pack* ) > MyFunction;
void addEvent( MyFunction f );
void triggerEvents( Pack* );
std::list< MyFunction > eventList;
void DNetwork::addEvent( MyFunction f )
{
eventList.push_back( f );
}
void DNetwork::triggerEvents( Pack *pack )
{
for ( auto it = eventList.begin(); it != eventList.end(); it++ )
{
(*it)(pack);
}
}
Now if I have the class A with the method doA( Pack* )
I will write:
A a;
Pack pack;
DNetwork d;
d.addEvent( std::bind( &A::doA, &a, &pack ) );
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:
A a;
Pack pack;
DNetwork d;
d.addEvent( std::bind( &A::doA, &a, std::placeholders::_1 ) );
d.triggerEvents( &pack );
The simple solution is using type erasure on the function/function pointer type, for which the easier way is just using std::function<>
:
std::list<std::function<void (Pack*)>;
Then you can initialize the function
objects with either a free function or a member function (by means of std::bind
to bind the member-function with an object on which to call it) or even function objects (types that offer an operator()
).
You could do an overload such as:
std::list<void(*)(Pack *)> eventList;
void addEvent(void (*func)(Pack *));
template<typename T>
void addEvent(void (T::*func)(Pack *));
namespace Abstraction {
template<typename T>
void abstractlyAddEvent( T, std::list<void(*)(Pack *)> *eventList );
}
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:
void * (TestClass:: *) ()