#ifndef _TOYPTR_H_

#define _TOYPTR_H_





template <typename T> class ToyPtr {

 public:

  // Constructor that accepts the ptr.

  ToyPtr(T *ptr) : ptr_(ptr) { }



  // NEW - Copy constructor.

  ToyPtr(const ToyPtr &copy) {

    copyPtr(copy);

  }



  // Destructor that deletes the ptr.

  ~ToyPtr() {

    if (ptr_ != nullptr) {

      delete ptr_;

      ptr_ = nullptr;

    }

  }



  // NEW - Implement the "=" operator

  ToyPtr &operator=(const ToyPtr &rhs) {

    if (this != &rhs)

      copyPtr(rhs);

    return *this;

  }



  // Implement the "*" operator

  T &operator*() {

    return *ptr_;

  }



  // Implement the "->" operator

  T *operator->() {

    return ptr_;

  }



 private:

  // NEW - Helper function to copy from another ToyPtr.

  void copyPtr(const ToyPtr &copy) {

    if (copy.ptr_ == nullptr) {

      if (ptr_ != nullptr)

        delete ptr_;

      ptr_ = nullptr;

    } else {

      if (ptr_ == nullptr)

        ptr_ = new T;

      *ptr_ = *copy.ptr_;

    }

  }

  // NEW - The pointer itself, default initialized to nullptr.

  T *ptr_ = nullptr;

};



#endif  // _TOYPTR_H_