Re: Insert object with const member in vector
On Jul 18, 4:13 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"James Kanze" <james.ka...@gmail.com> wrote in message
news:ed0ac11d-6bcd-4905-a17d-89a393ad940c@i28g2000yqa.googlegroups.com...
Standard containers have value semantics. They copy the
object inserted into them, and may use assignment to move it
around later. You have to decide whether your object should
have value semantics, or whether you want to give it
identity.
You have said this before and I think I have dismissed it
before but I will reiterate it again: it *is* possible for
container elements to have "identity" as you call it: consider
elements in a std::list, std::set or std::map, these elements
are not copied around.
Except when you insert them. The standard containers are based
on value semantics. There is a certain sense, of course, that
all objects have identity. But you can't construct an object
with identity in any of the standard containers.
For std::vector you can store a smart pointer of the object
which then has "identity".
You don't even need a smart pointer for this; a raw pointer will
do the trick. But the object in the container is a pointer, not
the object you're referring to, and that pointer does not have
identity.
You can (fairly easily, in fact) wrap std::vector with a class
which does maintain a vector of pointers, and hides this fact in
its interface. But that wouldn't be std::vector.
I define object "identity semantics" as the property that
external references to it remain valid no matter what
container operation is performed (modulo erasing the object in
question of course).
Identity is more than that. It literally means never copying,
at least within a transaction. Using a copy of the object is
not the same thing as using the object.
--
James Kanze