I am trying to create a map that contains member function pointers of different classes. The member functions all have the same signature. In order to do this all my classes inherit an Object class which only has default constructor, virtual destructor and a virtual ToString() const method.
// The map looks like this
map<Object*, void (Object::*)()> mapOfMethodPointers;
// And here are the two classes that inherit Object
// Class A
class A : public Object
{
public:
void AFunc();
string ToString() const override;
};
void A::AFunc()
{
cout << "AFunc()" << endl;
}
string A::ToString() const
{
cout << "A" << endl;
}
// Class B
class B : public Object
{
public:
void BFunc();
string ToString() const override;
}
void B::BFunc()
{
cout << "BFunc()" << endl;
}
string B::ToString() const
{
cout << "B" << endl;
}
// Here is how add the member function pointers in the map
A a;
B b;
mapOfMethodPointers[*a] = &A::AFunc;
mapOfMethodPointers[*b] = &B::BFunc;
When I add both of the member function pointers in the map I get the following errors:
- Can't convert 'void (B::*)()' to 'void (Object::*)()'
- Can't convert 'void (A::*)()' to 'void (Object::*)()'
Regardless of the fact that both class A and class B are Objects, I can't make this convertions. How can I achieve such thing? I need something like polymorphism for member function pointers. The implementation I chose doesn't work. Any ideas?