I am trying to transfer a function as a parameter to a different function.
for some reason it is not working.
//myClass.h
class MyClass
{
public:
typedef int (*MyClass::ptrToMember)(float, char);
ptrToMember p1;
MyClass::MyClass();
void hello(ptrToMember fun);
int SendIt (float a, char b);
};
//MyClass.cpp
MyClass::MyClass(){
p1 = &(MyClass::SendIt);
hello(p1);
}
int MyClass::SendIt (float a, char b)
{
std::cout << "MyClass::SendIt "<<a<<std::endl;
return 1;
}
void MyClass::hello(ptrToMember fun){
int result = (*fun)(12, 'a');
std::cout << result << std::endl;
}
would appreciate it, if someone can tell me what my mistake is.
Pointers to class member functions are declared like this:
And called like this (test is the class instance – can also be ‘this’):
or
The decleration operator is
::*, while the operator to invoke your pointer to a member function are.*and->*.Edit:
This line:
actually has to be: