Name

auto_ptr class template — Smart pointer to manage ownership of pointers

Synopsis

template <class T> struct auto_ptr_ref {};
template<class T>
class auto_ptr {
public:
  typedef T element_type;
  explicit auto_ptr(T* p = 0) throw(  );
  auto_ptr(auto_ptr&) throw(  );
  template<class U> auto_ptr(auto_ptr<U>&) throw(  );
  auto_ptr(auto_ptr_ref<T>) throw(  );
  ~auto_ptr(  ) throw(  );
  auto_ptr& operator=(auto_ptr&) throw(  );
  template<class U>
  auto_ptr& operator=(auto_ptr<U>&) throw(  );
  auto_ptr& operator=(auto_ptr_ref<T> r) throw(  );
   
  T& operator*(  ) const throw(  );
  T* operator->(  ) const throw(  );
  T* get(  ) const throw(  );
  T* release(  ) throw(  );
  void reset(T* p = 0) throw(  );
   
  template<class U> operator 
                  auto_ptr_ref<U>(  ) throw(  );
  template<class U> operator 
                  auto_ptr<U>(  ) throw(  );
};

The auto_ptr class template implements a smart pointer to manage ownership of pointers. Proper use of auto_ptr ensures that a pointer has exactly one owner (which prevents accidental double deletes), and the owner automatically frees the memory when the owner goes out of scope (which prevents memory leaks). Assignment of auto_ptr values transfers ownership from the source to the target of the assignment.

The auto_ptr_ref type holds a reference to an auto_ptr. Implicit conversions between auto_ptr and auto_ptr_ref facilitate the return of auto_ptr objects from functions. Usually, you can ignore the auto_ptr_ref type and let the implicit conversions handle the details for you. All you need to do is use auto_ptr as a ...

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.