auto_ptr Class

Ensures that the resource it points to is destroyed automatically when control leaves a block.

For a list of all members of this type, see auto_ptr Members.

The more capable unique_ptr Class class supersedes auto_ptr.

template <classType>
class auto_ptr

Parameters

  • Type
    The type of object for which storage is being allocated or deallocated.

Remarks

The template class describes an object that stores a pointer to an allocated object myptr of type Type *. The stored pointer must be either null or designate an object allocated by a new expression. An object constructed with a nonnull pointer owns the pointer. It transfers ownership if its stored value is assigned to another object. (It replaces the stored value after a transfer with a null pointer.) The destructor for auto_ptr<Type> deletes the allocated object if it owns it. Hence, an object of class auto_ptr<Type> ensures that an allocated object is automatically deleted when control leaves a block, even through a thrown exception. You should not construct two auto_ptr<Type> objects that own the same object.

You can pass an auto_ptr<Type> object by value as an argument to a function call. You can return such an object by value as well. Both operations depend on the implicit construction of intermediate objects of class auto_ptr<Type>::auto_ptr_ref<Other>, by various subtle conversion rules. You cannot, however, reliably manage a sequence of auto_ptr<Type> objects with a Standard Template Library container.

Requirements

Header:<memory>

Namespace: std

See Also

Reference

Thread Safety in the Standard C++ Library

Other Resources

auto_ptr Members

<memory> Members

unique_ptr Class