I\'ve been trying to implement a C#-like event system in C++ with the tr1 function templates used to store a function that handles the event.
I created a vector so
If you are storing function pointers only (and not other functors that match the signature required), this is easy (see code below). But in general, the answer, like other posters have said, is no. In that case, you probably want to store your functors in a hash, as values, with keys being something the user supplies on adding and removing.
The code below demonstrates how to get the functor/pointer object that is to be called. To use it, you must know the exact type of the object to extract (i.e., the typeid
of the type you specify must match the typeid
of the contained functor/pointer).
#include
#include
using std::printf;
using std::tr1::function;
int main(int, char**);
static function main_func(&main);
int
main(int argc, char** argv)
{
printf("%p == %p\n", *main_func.target(), &main);
return 0;
}