INSTRUCTIONS: In this assignment, you will make your own smart pointer type. You will write the following class to develop a referenced counted smart pointer. You will also implement a few other...



INSTRUCTIONS: In this assignment, you will make your own smart pointer type. You will write the following class to develop a referenced counted smart pointer. You will also implement a few other member functions to resemble the functionality of an ordinary raw pointer. Basically, this is a problem of designing a single, non-trivial class and overloading a few pointer related operators. You may not use any of the STLs except for stdexcept, to do this, i.e., please dont try and use a shared_ptr to implement the class. You can start with this code, and you may add other member functions, if you want.



C++ LANGUAGE




Only need smart_ptr.h file.


template <typename T><br>class smart_ptr {<br>public:<br>smart_ptr();<br>// Create a smart_ptr that is initialized to nullptr. The<br>// reference count should be initialized to nullptr.<br>explicit smart_ptr(T* raw_ptr);<br>// Create a smart_ptr that is initialized to raw_ptr. The<br>// reference count should be one. Make sure it points to<br>// the same pointer as the raw_ptr.<br>smart_ptr(const smart_ptr& rhs);<br>// Copy construct a pointer from rhs. The reference count<br>// should be incremented by one.<br>smart_ptr(smart_ptr&& rhs);<br>// Move construct a pointer from rhs.<br>smart_ptr& operator=(const smart_ptr& rhs);<br>// This assignment should make a shallow copy of the<br>// right-hand side's pointer data. The reference count<br>// should be incremented as appropriate.<br>smart_ptr& operator=(smart_ptr&& rhs);<br>// This move assignment should steal the right-hand side's<br>// pointer data.<br>bool clone();<br>// If the smart_ptr is either nullptr or has a reference<br>// count of one, this function will do nothing and return<br>// false. Otherwise, the referred to object's reference<br>// count will be decreased and a new deep copy of the<br>// object will be created. This new copy will be the<br>// object that this smart_ptr points and its reference<br>// count will be one.<br>int ref_count() const;<br>// Returns the reference count of the pointed to data.<br>T& operator* ( );<br>// The dereference operator shall return a reference to<br>// the referred object. Throws null_ptr_exception on<br>// invalid access.<br>T* operator->();<br>// The arrow operator shall return the pointer ptr_.<br>// Throws null_ptr_exception on invalid access.<br>~smart_ptr(); // deallocate all dynamic memory<br>private:<br>T* ptr_; // pointer to the referred object<br>int* ref_; // pointer to a reference count<br>};<br>

Extracted text: template class smart_ptr { public: smart_ptr(); // Create a smart_ptr that is initialized to nullptr. The // reference count should be initialized to nullptr. explicit smart_ptr(T* raw_ptr); // Create a smart_ptr that is initialized to raw_ptr. The // reference count should be one. Make sure it points to // the same pointer as the raw_ptr. smart_ptr(const smart_ptr& rhs); // Copy construct a pointer from rhs. The reference count // should be incremented by one. smart_ptr(smart_ptr&& rhs); // Move construct a pointer from rhs. smart_ptr& operator=(const smart_ptr& rhs); // This assignment should make a shallow copy of the // right-hand side's pointer data. The reference count // should be incremented as appropriate. smart_ptr& operator=(smart_ptr&& rhs); // This move assignment should steal the right-hand side's // pointer data. bool clone(); // If the smart_ptr is either nullptr or has a reference // count of one, this function will do nothing and return // false. Otherwise, the referred to object's reference // count will be decreased and a new deep copy of the // object will be created. This new copy will be the // object that this smart_ptr points and its reference // count will be one. int ref_count() const; // Returns the reference count of the pointed to data. T& operator* ( ); // The dereference operator shall return a reference to // the referred object. Throws null_ptr_exception on // invalid access. T* operator->(); // The arrow operator shall return the pointer ptr_. // Throws null_ptr_exception on invalid access. ~smart_ptr(); // deallocate all dynamic memory private: T* ptr_; // pointer to the referred object int* ref_; // pointer to a reference count };
Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here