Re: naked pointer vs boost::shared_ptr<T>
On Mar 8, 4:04 pm, "Dejan.Mircev...@gmail.com"
<Dejan.Mircev...@gmail.com> wrote:
On Mar 8, 6:54 am, "James Kanze" <james.ka...@gmail.com> wrote:
using smart
pointers in such cases is pretty much lying to the reader, since
there is in fact no "shared ownership".
Are you equating "smart pointer" with shared_ptr?
Not necessarily, but it is the only one of the usual lot which
would seem appropriate here.
I am not. To me,
"smart pointers" include all of boost/smart_ptr.hpp and <memory>. By
choosing properly among them, I never lie to the reader.
So which one says that the object has an explicit lifetime, that
it manages itself, and that users of the object must register
with it, so that they can be correctly notified of changes
and/or deletion?
In particular, weak_ptr doesn't imply shared ownership.
But it does imply that I can create a shared_ptr, and thus
obtain shared ownership. A lie, in sum.
It conveys exactly the intent you use the raw pointers for,
but it doesn't allow you to accidentally free the memory or
dereference NULL. This is why your claim that raw pointer is
better baffles me.
Because it doesn't lie. A weak_ptr (at least a boost::weak_ptr)
supposes a shared_ptr somewhere.
If an exception (or anything else)
causes the registry to be destructed, and I'm still using it, my
code will crash.
Some people prefer to catch the exception and try to recover,
particularly in server code.
You can't recover in such a case. By definition. You've lost
it.
That was the situation I was considering.
Why consider impossibilities? Why not engineer the thing
correctly to begin with?
You can't continue using the registry then, since you
can't reference it.
In fact, the registry is
generally a singleton, or a member of a singleton, and created
in such a way that it will never be destructed.
Then your identity objects can never outlive it and can comfortably be
owned by it through shared_ptr. Just don't copy shared_ptr's when you
don't intend to share.
Putting shared_ptr in the registry is a lie, since the registry
doesn't own the object. There's no sharing. There's no
ownership. Anything which forces something which looks like
ownership is lying to the reader.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]