I have a problem with storing lambda expression with capturing "this" pointer in class as parameter. I made typedef like this:
typedef void(*func)();
This code below works fine.
#include <iostream>
using namespace std;
typedef void(*func)();
class A {
public:
func f;
};
class B {
public:
A *a;
B() {
a = new A();
a->f = [](){
printf("Hello!");
};
}
};
int main() {
B *b = new B();
b->a->f();
return 0;
}
There is no capturing yet but when I want to capture "this" pointer in lambda it throws an error. How can I make typedef with capturing? I want to do something like this:
#include <iostream>
using namespace std;
typedef void(*func)();
class A {
public:
func f;
};
class B {
public:
A *a;
B() {
a = new A();
//There is a problem with [this]
a->f = [this](){
//Method from class B using "this" pointer
this->p();
};
}
void p() {
printf("Hello!");
}
};
int main() {
B *b = new B();
b->a->f();
return 0;
}
What am I doing wrong? Explain me please. Thanks.