Categories
技术讨论

auto_ptr的应用

template<class T>
class auto_ptr {
public:
typedef T element_type;
explicit auto_ptr(T *p = 0) throw();
auto_ptr(const auto_ptr<T>& rhs) throw();
auto_ptr<T>& operator=(auto_ptr<T>& rhs) throw();
~auto_ptr();
T& operator*() const throw();
T *operator->() const throw();
T *get() const throw();
T *release() const throw();
};
The class describes an object that stores a pointer to an allocated object
of type T. The stored pointer must either be null or designate an object
allocated by a new expression. The object also stores an ownership
indicator. An object constructed with a non-null pointer owns the pointer.
It transfers ownership if its stored value is assigned to another object.
The destructor for auto_ptr<T> deletes the allocated object if it owns it.
Hence, an object of class auto_ptr<T> ensures that an allocated object is
automatically deleted when control leaves a block, even via a thrown
exception.

—–
可以自动释放对象。

auto_ptr<C> p(new C());
p->DoSomething();

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.