Re: Which is best way to store objects?
On Jan 3, 5:50 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Jeff Flinn" <TriumphSprint2...@hotmail.com> wrote in message
news:hhqdlo$4vv$1@news.eternal-september.org...
James Kanze wrote:
On Jan 2, 10:45 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
Which makes one ask questions. Since pointers (raw or
otherwise) have different semantics than using the
objects themselves. You simply can't replace one with
the other.
If you wanted a container of polymorphic objects (i.e.
std::vector<base*>) then shared_ptr is ideal.
That's very debatable. Most polymorphic objects are entity
objects, for which the semantics of shared_ptr are not
appropriate.
Why not? Can you expound on that statement.
I assume he means you should not be using something with
shared ownership semantics if it is only owned by one
container.
Actually, I'd argue against that, because the name implies
sharing, and there is no sharing involved. Still, in the
absence of other, better named tools, it will do, provided it is
hidden, as an implementation detail.
This is why I said unique_ptr is actually ideal (but isn't yet
available so shared_ptr suffices) or use the Boost pointer
containers.
For such cases, I agree. But how often does a container "own"
an object. (Formally, I'd argue never. If not, it's not just a
container, but something more. But again, it's often
convenient, from an implementation point of view, to allow it to
do so.)
Reinventing the wheel to ensure objects are deleted is far
from ideal.
There's no necessity of reinventing the wheel, since no special
tool is necessary. Most objects have deterministic lifetimes.
--
James Kanze