casting a void* to a pointer to a member function
This is driving me nuts! Here's my problem, I'll illustrate by example here for clarity.
In brief, what I want to do is pass the address of a member function of a child class into a function of it's base class.
But the compiler insinuates that this just isn't possible because child::function does not match parameter parent::function.
I have tried all sorts of casts but the compiler stands firm.
So my idea was to define the parameter i the parent class as type void*.
Inside the function, I could then cast the child::function into a parent::function beneath the prying eyes of the compiler, and thus go about my day.
But now it just says "cannot convert 'function' from type 'void *' to type 'void (Parent::*)()'..."
example:
Code:
class Parent {
public:
void (Parent:: *invoke)(void);
void AttachA(void (Parent:: *function)(void)) {
invoke = function;
}
void AttachB(void * function) {
invoke = (void (Parent:: *)(void)) function;
}
};
class Child : public Parent {
public:
void ThisFunction() {
}
void Attach() {
AttachA(&Child::ThisFunction); //...error: no matching call to AttachA(void (Child::(*)())...(canidates are "Parent::")...
AttachA(&Parent::ThisFunction); //...error: ThisFunction not a member of parent...
AttachB((void*)ThisFunction); //..error: cannot resolve based on conversion to type void*...
}
};
Anyone know a good way around that?