/*
* Copyright ©2026 Soham Pardeshi. All rights reserved.
* Permission is hereby granted to students registered for University of
* Washington CSE 333 for use solely during Summer Quarter 2026 for
* purposes of the course. No other use, copying, distribution, or
* modification is permitted without prior written consent. Copyrights
* for third-party components of this work must be honored. Instructors
* interested in reusing these course materials should contact the author.
*/
#ifndef TOYPTR_H_
#define TOYPTR_H_
// A minimal "smart" pointer: it owns a heap object and deletes it in its
// destructor, so the object is freed when the ToyPtr goes out of scope.
// This is RAII, and it is all a smart pointer fundamentally is.
//
// Note what is MISSING: a copy constructor and an assignment operator.
// The compiler-synthesized versions copy ptr_ shallowly, so two ToyPtrs
// end up owning the same object and both delete it. See toyuse.cc.
template <typename T>
class ToyPtr {
public:
explicit ToyPtr(T* ptr) : ptr_(ptr) {}
~ToyPtr() { delete ptr_; } // delete nullptr is safe
T& operator*() { return *ptr_; }
T* operator->() { return ptr_; }
private:
T* ptr_;
};
#endif // TOYPTR_H_