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...

1 answer below »

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 STL, except for stdexcept ("So what you're saying is 'exceptions is the only exception'?" - Binh) to do this, i.e., please don't 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.


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. No change is made to raw_ptr.            explicit smart_ptr(T* &&raw_ptr);       // Create a smart_ptr that is initialized to raw_ptr. The reference count       // should be one. If the constructor fails raw_ptr is deleted.      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 };

Here is an example of how the above class might work.



int* p { new int { 42 } }; smart_ptr sp1 { p }; cout sp2 { sp1 }; cout sp3; cout sp4 = std::move(sp1); cout


The arrow operator will only compile and work if the referred object is a class type



struct Point { int x = 2; int y = -5; }; int main ( ) { smart_ptr sp { new Point }; cout < sp-="">x < "="" "="">< sp-="">y

Here is an example of theclonemember function



smart_ptr dsp1 { new double {3.14} }; smart_ptr dsp2, dsp3; dsp3 = dsp2 = dsp1; cout

Requirements/Hints


Here are some of the requirements for writing the class. Test the implemented member functions to verify their basic functionality, and also how much of the desired semantics that is achieved by this implementation, and also if there are any undesired effects.



  1. Thenull_ptr_exceptionis an exception that you will define, it should be derived from an STL exception.

  2. Label the above member functions asnoexceptwhere appropriate.

  3. You may add additional member functions, if you'd like.

  4. Recognize that move constructors/assignments result in the reference count remaining the same, hence there's no need to change it.

  5. Think about writing as exception-safe code as possible. If a function can throw try and give it a strong guarantee if possible.


What to turn in for this Project:


What you will turn in for this assignment is a zip file containing one file:



  • A text file namedsmart_ptr.cppthat contains the source code for your C++ program. Your source code should have helpful comments that tell the purpose of the major program segments and explain any tricky code.



Answered 1 days AfterMar 29, 2021

Answer To: you will make your own smart pointer type. You will write the following class to develop a...

Pulkit answered on Mar 30 2021
136 Votes
sol/smart_ptr.cpp
sol/smart_ptr.cpp
template 
class smart_ptr {
public:
    smart_pt
r();
      // 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.
    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 co...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here