Car Signs Containers
The problem lies in the semantics of copying in auto_ptr
. When you assign two automatic pointers, RHS will transfer ownership of the LHS. This means that the assignment signature is: auto_ptr& operator=( auto_ptr& rhs )
(note that there is not in RHS const
), so in many cases it won't even compile.
There are other smart pointers that can be used with containers. In TR1 there is shared_ptr
, modeled after boost::shared_ptr
(in some compilers, the code in boost is just copied and with changed namespaces). Boost also has boost::unique_ptr
one that will replace auto_ptr
in the upcoming standard. It models a single property with move semantics, so it can be used internally at no additional cost to use shared_ptr
(not so that the cost is noticeable in most cases).
a source to share