Thanks Stefan & Peter, for your replies. I tried immediately: struct MyTaskStruct : public LongTaskStruct , TaskDoneStruct , boost::enable_shared_from_this<MyTaskStruct> { MyTaskStruct() { setCallback(shared_from_this()) ; } ... } And obviously, I got: terminate called after throwing an instance of 'boost::bad_weak_ptr' what(): boost::bad_weak_ptr precisely because:
but you can't do this in a constructor, because the outer shared_ptr<> hasn't been created yet.
But the next solution
Use a static create() function as explained here :
is not good enough. I've got 150+ Java classes to translate, each with an average of about 2 constructors. That would mean an extra 300 createXYZ, and apart from being ugly, and an insult to the user, is a maintenance nightmare. Ideally, I would want an intrusive_ptr (because no matter how they are created, they can be made to share a single ref-count per body/ instance), but one that offers the shared_ptr guarantee: shared_ptr<T> can be implicitly converted to shared_ptr<U> whenever T* can be implicitly converted to U*. In particular, shared_ptr<T> is implicitly convertible to shared_ptr<T const>, to shared_ptr<U> where U is an accessible base of T, and to shared_ptr<void>. What I am after, is a construct as transparent as possible, for which I can say, in my documentation, "For every struct XXX -- the body --, there is a corresponding envelope -- xxxx_ptr<XXX> -- that walks and talks like a XXX *" So far, shared_ptr had everything covered except that "sharing" thing, which is an artefact, not a requirement. intrusive_ptr makes this "sharing" thingie a non issue (as it should be), but fails on the "polymorphic covariance" quoted above... In this project, it is acceptable to rely on a common base class, (anyway I've got to translate Java's "Object" class :-) so I can stuff it with whatever machinery is required to make garbage collection work. Any idea? Many thanks. -- JFB