auto_ptr Class


Wraps a smart pointer around a resource that ensures the resource is destroyed automatically when control leaves a block.

The more capable unique_ptr class supersedes auto_ptr. For more information, see unique_ptr Class.

For more information about throw() and exception handling, see Exception Specifications (throw) (C++).

template<class Type>
    class auto_ptr {
    typedef Type element_type;
    explicit auto_ptr(Type *_Ptr = 0) throw();
    auto_ptr(auto_ptr<Type>& _Right) throw();
    template<class Other>
        operator auto_ptr<Other>() throw();
    template<class Other>
        auto_ptr<Type>& operator=(auto_ptr<Other>& _Right) throw();
    template<class Other>
        auto_ptr(auto_ptr<Other>& _Right);
    auto_ptr<Type>& operator=(auto_ptr<Type>& _Right);
    Type& operator*() const throw();
    Type *operator->()const throw();
    Type *get() const throw();
    Type *release()throw();
    void reset(Type *_Ptr = 0);


The auto_ptr from which to get an existing resource.


The pointer specified to replace the stored pointer.

The template class describes a smart pointer, called an auto_ptr, to an allocated object. The pointer must be either null or designate an object allocated by new. The auto_ptr 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. The 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. An auto_ptr cannot be an element of any Standard Library container. You cannot reliably manage a sequence of auto_ptr<Type> objects with a Standard Template Library container.


The constructor for objects of type auto_ptr.


The type is a synonym for the template parameter Type.


The member function returns the stored pointer myptr.


The member replaces the stored pointer myptr with a null pointer and returns the previously stored pointer.


The member function evaluates the expression delete myptr, but only if the stored pointer value myptr changes as a result of function call. It then replaces the stored pointer with ptr.


An assignment operator that transfers ownership from one auto_ptr object to another.


The dereferencing operator for objects of type auto_ptr.


The operator for allowing member access.

operator auto_ptr<Other>

Casts from one kind of auto_ptr to another kind of auto_ptr.

operator auto_ptr_ref<Other>

Casts from an auto_ptr to an auto_ptr_ref.


Header: <memory>

Namespace: std