Share via


auto_ptr Class

The template class describes an object that stores a pointer to an allocated object of type Type* that ensures that the object to which it points gets destroyed automatically when control leaves a block.

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

template <class Type> 
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 either be 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