The object on the left-hand side takes ownership of the pointer (i.e., it is now in charge of freeing the memory block when destroyed). The auto_ptr object on the right-hand side is automatically released (i.e., it is set to point to the null pointer) after the copy.
Notice however that the left-hand side object is not automatically deallocated when it already points to some object. You can explicitly do this by calling member function reset before assigning it a new value.
Parameters
- a
- An auto_ptr object.
When the types held by the origin and destination auto_ptrs are different (second constructor version), an implicit conversion must exist between their pointer types. - r
- An auto_ptr_ref object (a reference to auto_ptr).
X is auto_ptr's template parameter (i.e., the type pointed).
Return value
A pointer to the element pointed (same as member auto_ptr::get).Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Output:
p2 points to 11 |
See also
| auto_ptr::reset | Deallocate object pointed and set new value (public member function) |
| auto_ptr::auto_ptr | Construct auto_ptr object (public member function) |
| auto_ptr::get | Get pointer (public member function) |
| auto_ptr::release | Release pointer (member function) |
