I want to create an array of functions to member functions that have the same signature but are of different classes. (But they are inherited). I tried something like this and I understand why it doesn't work but how can I work around that and still be able to do this? I don't mind if for example the function doesnt exist on the object I pass (if it is of another class) since I have logic for that.
class Base
{
public:
virtual void BaseFunc();
};
class X : Base
{
public:
void XFunc();
};
class Y : Base
{
public:
void YFunc();
};
int main()
{
std::vector<std::function<void(Base*)>> v;
v.push_back(&Base::BaseFunc); //OK
v.push_back(&X::XFunc); //NOT OK
v.push_back(&Y::YFunc); //NOT OK
}
I also tried having a template class that has a pointer to a member function of that class and then I would call from that class, but I can't have an array of templated class without giving the template argument(even if the class is simple and size doesn't depend on template argument). I also tried having the array just of void pointers but I found out that it is impossible to cast member function pointer to a void pointer.(I may not be right so if it is possible let me know)
Edit: To provide some background about my intent I'll paste my comment here: My idea is to let the user implement functions in a class that inherits a base class and then be able to pass these functions to my class which then stores them in an array and calls them on certain conditions that are specified.
Edit 2: I need a solution that is in c++11 or c++14