I've been going nuts trying to figure this out. Consider the following code (I'm assuming forward references have been defined):
// Signature representing a pointer to a method call
typedef
void (MyClass::*MyMethod)(int);
class MyClass
{
MyClass();
void method1(int i);
void method2(int i);
void associateMethod(int index, MyMethod m);
}
Given the above, the constructor can do things like the following:
MyClass::MyClass()
{
associateMethod(1, &MyClass::method1);
associateMethod(2, &MyClass::method2);
}
However, I'd like to be able to invoke 'associateMethod' where the second parameter is an anonymous method. However, the following doesn't compile.
associateMethod(3, [this](int) -> void { /* some code here */ }
I get an error about their being no viable conversion from the lambda to MyMethod.
I'm wondering if the lambda syntax needs to include 'MyClass' somewhere but random guesses for the lambda expression such as
MyClass::[this](int) -> void {}
or
[this]&MyClass::(int) -> void {}
don't compile.
Would appreciate any pointers (no pun intended)
Thanks