shared_from_this and Capture Lists
In many locations lambdas are used that capture this
MyClass::MyClass()
{
auto func = [&]()
{
this->doSomething();
};
}
The above is fine but if this function ptr is to be passed around it could exist longer then the duration of the class, for which any reference to anything to this
in that function would fail, likely a read access violation.
With shared_from_this in Components we can make this safer.
MyClass::MyClass()
{
auto strongThis = shared_from_this();
auto func = [strongThis]()
{
strongThis->doSomething();
};
}
This however, won't work because we cannot use shared_from_this in a constructor. For this reason it may be appropriate in Behaviours to have a separate function to initialize internal state. Particularly in a Behaviour that uses TaskNode's which rely on function ptrs.