Jean-François Brouillet wrote:
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.
Insult to the user? Ugly? There is little difference between SomethingPtr p( new Something ); and SomethingPtr p = Something::create(); from user point of view, and it hides the explicit new.
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>.
intrusive_ptr<T> is convertible to intrusive_ptr<U> when T* is convertible to U*. It doesn't support void, of course, but neither does Java. You'll probably need a common base class in which to put the reference count, though... either that (which implies virtual public inheritance to avoid multiple copies of the reference count), or an interface with add_ref and release that is implemented in every root concrete class (since Java only supports single implementation inheritance, multiple counts won't be a problem.) I prefer the static create function, but to each his own. ;-)