Condividi tramite


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.

template<class Type>
    class auto_ptr {
public:
    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);
    ~auto_ptr();
    Type& operator*() const throw();
    Type *operator->()const throw();
    Type *get() const throw();
    Type *release()throw();
    void reset(Type *_Ptr = 0);
};

Parameters

  • _Right
    The auto_ptr from which to get an existing resource.

  • _Ptr
    The pointer specified to replace the stored pointer.

Remarks

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.

Members

Constructors

auto_ptr

The constructor for objects of type auto_ptr.

Typedefs

element_type

The type is a synonym for the template parameter Type.

Member Functions

get

The member function returns the stored pointer myptr.

release

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

reset

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.

Operators

operator=

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

operator*

The dereferencing operator for objects of type auto_ptr.

operator->

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.

Requirements

Header:<memory>

Namespace: std

See Also

Reference

Thread Safety in the C++ Standard Library

unique_ptr Class