Question: Smart pointers are designed to make dynamic memory more accessible and safe to use. They (i.e., smart pointers) are implemented as wrappers around raw pointers,

Smart pointers are designed to make dynamic memory more accessible and safe to use. They (i.e., smart pointers) are implemented as wrappers around raw pointers, providing behaviors reminiscent of the raw pointers you’re so familiar with, while safeguarding against the pitfalls and opportunities for errors that you’ve probably experienced. We will provide a more complete introduction to smart pointers later in this semester.

Unique pointer 

we will provide a focused introduction to the unique pointer. The mantra of the unique pointer is exclusive ownership and accordingly implements a move-only type. The unique pointer implementation utilizes the resource acquisition is initialization (RAII) programming idiom, where the lifetime of a dynamically allocated object (i.e. the resource) is tied to the unique pointer object that owns it.

The allocation of the dynamically allocated object (i.e. resource acquisition) owned by the unique pointer happens during a construction (initialization). Deallocation of that object is always completed during destruction. A non-null unique pointer, by virtue of its name, always owns what it points to. Therefore, two objects of this type will never share the same resource. This invariant is maintained by disabling the copy mechanisms for this type.

In this activity

You will write a simplified implementation of a unique pointer, which we will call UniquePtr. Objects derived from this blueprint will maintain a unique pointer to a dynamically allocated object of type T.

Function Signatures and Behaviors

  • UniquePtr(const T& data);
    Constructs a UniquePtr, initializing holder_ with a new dynamically allocated object of type T initialized with value data.
  • UniquePtr(const UniquePtr& lhs);
    Deleted.
  • ~UniquePtr();
    Deallocates the dynamically allocated object pointed to by holder_.
  • UniquePtr& operator=(const UniquePtr& lhs);
    Deleted.
  • T& operator*();
    Returns a reference to the object pointed to by holder_.

UML Class Diagram

class UniquePtr
- holder_ : T*
+ UniquePtr(data : const T&)
+ UniquePtr(lhs: const UniquePtr&)
+ ~UniquePtr( )
+ operator=(lhs: const UniquePtr&) : UniquePtr&
+ operator*( ) : T&

1 #include 2 3 int main () { 4 //testing 5 std::cout < < "Let's implement a UnqiuePtx < < std::endl; 6} 7 11 naive-unique-pointer driver.cc unique-ptr.hpp unique-ptr.cc > Click run program to compile and run 

1 #include 2 3 int main () { 4 //testing 5 std::cout < < "Let's implement a UnqiuePtx < < std::endl; 6} 7 11 naive-unique-pointer driver.cc unique-ptr.hpp unique-ptr.cc > Click run program to compile and run your code. Click grade code to submit and grade your code. CS 128 A+ Editor Run Grade 8 D

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

SOLUTION I have solved this problem in C code with comments for easy understanding Below is the ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!