// -*-c++-*-
#ifndef _auto_ptr_h
#define _auto_ptr_h

#include <iostream.h>
#include <cstdlib>

template<class T>
class auto_ptr {
public:
  typedef T element_type;

  // explicit makes it so this constructor can't be called implicitly,
  // so it won't implicitly convert a T* to auto_ptr<T>.  
  explicit auto_ptr(T* p=0) : _ptr(p) {}
  auto_ptr(const auto_ptr<T>& r)  {
    _ptr = const_cast<auto_ptr<T>&>(r).release();
  }
  
  auto_ptr& operator=(const auto_ptr<T>& r) {
    if (this != &r) {
      delete _ptr;
      _ptr = const_cast<auto_ptr<T>&>(r).release();
    }
    return *this;
  }
  ~auto_ptr() { delete _ptr; }

  T& operator*()  const { return *_ptr; }
  T* operator->() const { return _ptr; }
  T* get()        const { return _ptr; }
  T* release()    { T *tmp = _ptr; _ptr = 0; return tmp; }

private:
  T* _ptr;
};


#endif  // _auto_ptr_h
