I'm pretty new to C++ (after many Java years). I'm trying to use a function pointer to a member function of my class like this:
class MyClass
{
public:
MyClass();
void foo();
void bar();
};
MyClass::MyClass(){}
void MyClass::bar(){}
void MyClass::foo()
{
void (MyClass::*myMethod)();
myMethod = &MyClass::bar;
//-----THIS IS THE LINE WITH PROBLEM-----------
myMethod();
}
However, the compiler fails:
test.cpp: In member function ‘void MyClass::foo()’:
test.cpp:22:14: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘myMethod (...)’, e.g. ‘(... ->* myMethod) (...)’
myMethod();
^
I've been trying various mix of *, & characters, and googled for solutions, but cannot get it to work.
Any idea?