I suggest that you pass the this
pointer explicitly to the function, something like this
(I took the liberty of making a
public and initializing it to 0, for simplifying things, but it has nothing to do with your question):
#include <iostream>
#include <functional>
#include <memory>
class A
{
public:
int a;
public:
std::function<void(A& self)> lambdaFunc;
A(std::function<void(A& self)> lambdaParam) : lambdaFunc(lambdaParam), a(0)
{ /* nothing */ }
};
void someFunctionCall(std::shared_ptr<A> p)
{
std::cout << p->a << std::endl;
p->lambdaFunc(*p);
std::cout << p->a << std::endl;
}
int main()
{
someFunctionCall(std::shared_ptr<A>(new A([](A&self){ self.a=42; })));
}
Then, the to call the function for an object a
you'll have to write a.lambdaFunc(a)
, but you can wrap this up in a method, if you think it's not convenient.
The program (in C++11) prints 0, then 42.