M
Mark
can someone clear up the following for me.
I've read somewhere that auto_ptrs cannot be used with standard containers
as they
don't meet the requirements for equivalence. That is "copies are not
equivalent".
First what does that mean.
I know this much: auto_ptrs maintain an ownship flag which is set/unset
during a copy
operation so that the following code
auto_ptr<myclass> pCopy2 = pCopy1;
would leavel pCopy2 owning the pointer to the class, and pCopy1 not owning
it.
Only pCopy2 would have the right to delete the object pointed to, which is
enforced in the destructor for auto_ptr.
What I don't understand is how this prevents us from using it in a standard
container
such as a map. Is it this? When a copy of an auto_ptr is inserted into a
map, although
the copy inside the map becomes the rightful owner of the pointer ( which
would
initially seem fine ) anytime we retrieve the pointer from the map, that
pointer becomes
invalidated meaning that we would have to reinsert the pointer back into the
map after
using it to transfer ownship back into the map. While this does'nt break
anything, if we
remember to do it is inconvenient.
Any insights appreciated.
Mark
I've read somewhere that auto_ptrs cannot be used with standard containers
as they
don't meet the requirements for equivalence. That is "copies are not
equivalent".
First what does that mean.
I know this much: auto_ptrs maintain an ownship flag which is set/unset
during a copy
operation so that the following code
auto_ptr<myclass> pCopy2 = pCopy1;
would leavel pCopy2 owning the pointer to the class, and pCopy1 not owning
it.
Only pCopy2 would have the right to delete the object pointed to, which is
enforced in the destructor for auto_ptr.
What I don't understand is how this prevents us from using it in a standard
container
such as a map. Is it this? When a copy of an auto_ptr is inserted into a
map, although
the copy inside the map becomes the rightful owner of the pointer ( which
would
initially seem fine ) anytime we retrieve the pointer from the map, that
pointer becomes
invalidated meaning that we would have to reinsert the pointer back into the
map after
using it to transfer ownship back into the map. While this does'nt break
anything, if we
remember to do it is inconvenient.
Any insights appreciated.
Mark