Appearance
Simple unique_ptr
Overview
Your simplified implementation should provide:
- Template class to work with any type
- Constructor that takes ownership of a raw pointer
- Destructor that automatically cleans up the resource
- Move constructor and move assignment for transferring ownership
- Deleted copy operations to enforce exclusive ownership
- Access operators (
*and->) for using the managed object - Utility methods like
get()andreset()
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
};