最新消息:

auto_ptr的应用

技术讨论 hamo 947浏览 0评论

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();

转载请注明:哈默博客 » auto_ptr的应用

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址