13 Sep
2011
13 Sep
'11
1:21 a.m.
On 9/12/2011 12:41 PM, Igor R wrote:
I don't know what you mean by that.
I mean that you can pass a weak_ptr along with the binder:
void setYourFunctor(const function
&func, const weak_ptr<void> &track) { // store "func" and "track" func_ = func; track_ = track; } // Now in the place where "func_" should be called: void call() { shared_ptr<void> p = track_.lock(); if (p) func_(); }
// Now use it this way: shared_ptr<YourClass> ptr = ...; setYourFunctor(bind(&YourClass::doSomething, ptr.get()), ptr);
I see: assume that the raw pointer value doesn't change, and just use the weak ptr to ensure that it's still valid, but it is not necessary to pass the pointer to the call each time.