How can I pass a class member function to a non-class member function’s parameter? I am getting the following compile error:
error: argument of type ‘void (MyNamespace::MyClass::)(int*)’ does not match ‘void (*)(int*)’
Library.h
typedef void (*testCallback)(int* iParam);
void UsingTheFoo(testCallback Callback);
Library.cpp
void UsingTheFoo(testCallback Callback)
{
int *p_int = new int;
*p_int = 5;
Callback(p_int);
}
MyClass.cpp
namespace MyNamespace
{
void MyClass::fooCB(int* a)
{
printf("hey! %d\n", *a);
}
void MyClass::testo()
{
UsingTheFoo(fooCB);
}
} // MyNamespace
I can not change the code in “Library”, I need to use “UsingTheFoo” in MyClass member functions. I am aware my way is wrong, I searched and found similar questions but couldn’t understand (braindead at the end of shift 🙂 ) any of them completely to adapt for my problem.
I’ve already read: http://www.parashift.com/c++-faq-lite/pointers-to-members.html :S
Make method that you wanna to pass as a callback as static. You’ve got this error because all methods have implicitly first parameter – pointer to an object, this, so their prototype doesn’t correspond to callbacks’.