共用方式為


auto_ptr Class

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

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

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.

Requirements

Header:<memory>

Namespace: std

See Also

Reference

Thread Safety in the Standard C++ Library

unique_ptr Class

Other Resources

auto_ptr Members

<memory> Members