Use original work!! or will downvote* 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++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 17PE
icon
Related questions
Question

*Use original work!! or will downvote*

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.

*Use original work!! or will downvote*

template <typename T>
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
};
Transcribed Image Text:template <typename T> 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 };
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning