Hello,
I need some help on this problem :
I would like to be able to register pointers to member functions (with a defined signature; lets say : <int, std::string>) of different classes without knowing in advance those classes. So as long as the class have a member function with the good arguments, I want to be able to add a pointer to this member function into a container (the same for all classes).
As I know that all the registered functions wait for the same arguments, I don't care to know what is the class, because all I will need is these member function. But as the signature is different I don't know of to achieve this container of member function pointers.
I need this for an Event driven system. The member function of any kinds of objects (so they are unknown in advance... and even if the were known, I can't register each type of object to the EventDispatcher class) can register as a listener for a specific event on the EventDispatcher, and if this event is fired then we call back the registered member function with the Event as argument.
Let's try with a simple example :
Lets imagine two classes MyClassA and MyClassB that both have a member function with the same arguments ( int, std::string).
I would like to be able to add the pointers to those member functions in the same container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
class MyClassA
{
public:
MyClassA(){};
~MyClassA(){};
void myMemberFunctionA( int, std::string ) {};
};
class MyClassB
{
public:
MyClassB(){};
~MyClassB(){};
void myMemberFunctionB( int, std::string ) {};
};
/* Here is my problem ...
Compiler request me to define the container with a specific class name
(here MyClassA for the purpose of the example)... but I would like to be
able to add member function pointers of any kind of classes as long as
the member function signature is (int, std::string)
*/
// map < key, pair< memberFunction_ptr, classInstance_ptr > >
std::map< int, std::pair< void(MyClassA::*)(int, std::string), MyClassA* > > myMap;
// This does work
MyClassA a;
myMap.insert( myMap.size()+1, std::make_pair( &MyClassA::myMemberFunctionA, &a ) )
// This does NOT work (as myMap is not defined using MyClassB)
MyClassB b;
myMap.insert( myMap.size()+1, std::make_pair( &MyClassB::myMemberFunctionB, &b ) )
|
Indeed I could add MyClassC, MyClassD etc member function pointers later as long as the member function signature is still the same.
Do you have any idea, or clue the help me with this ?
Thank you