On Tuesday 06 February 2007 14:25 pm, Frank Mori Hess wrote:
On Tuesday 06 February 2007 10:23 am, Douglas Gregor wrote:
Does enable_shared_from_this help at all?
You know I think it might, at least using a modified or derived version of it. Apparently, shared_ptrs will call into objects derived from enable_shared_from_this in order to set their _internal_weak_this weak_ptr. If we could hook into that call, we could have it call a virtual function called postconstruct(), and voila! We have a postconstructer where signals can be connected to member functions with tracking, and there is no danger of a slot being called before the object is fully constructed. Subclassing works relatively well too, the subclass just has to be sure to call its base class' postconstruct() call if it overrides it.
I've made some minor changes to shared_ptr.hpp and enable_shared_from_this.hpp as a proof of concept (diffs against boost 1.32 attached). It appears to work beautifully. The "real thing" would also take steps to make sure the postconstructor only gets called once (maybe in a "postconstructible" class derived from enable_shared_from_this. And the virtual function that is called in enable_shared_from_this might want a less specific name than postconstruct. Peter, is there any chance a change like this could get put in shared_ptr? -- Frank