Skip to content

Simple unique_ptr

Overview

Your simplified implementation should provide:

  1. Template class to work with any type
  2. Constructor that takes ownership of a raw pointer
  3. Destructor that automatically cleans up the resource
  4. Move constructor and move assignment for transferring ownership
  5. Deleted copy operations to enforce exclusive ownership
  6. Access operators (* and ->) for using the managed object
  7. Utility methods like get() and reset()

Implement a simplified version of std::unique_ptr called 'MyUniquePtr'. It should be a class template that manages exclusive ownership of a dynamically allocated object.

cpp
#include <utility>

// TODO: Implement a simplified version of std::unique_ptr called 'MyUniquePtr'
// 
// Requirements:
// 1. Template class that manages exclusive ownership of a dynamically allocated object
// 2. Constructor taking a raw pointer (explicit to prevent accidental conversions)
// 3. Destructor that automatically deletes the managed object
// 4. Move constructor and move assignment operator (transfer ownership)
// 5. Deleted copy constructor and copy assignment (exclusive ownership)
// 6. Operator* for dereferencing the managed object
// 7. Operator-> for accessing members of the managed object
// 8. get() method that returns the raw pointer without transferring ownership
// 9. reset() method that deletes current object and optionally takes ownership of new one
//
// This demonstrates:
// - Template class design with type parameters
// - RAII resource management principles
// - Move-only semantics for exclusive ownership
// - Operator overloading for intuitive interface

template<typename T>
class MyUniquePtr {
    // Your implementation here
};