Implement a shared pointer to an int
in a class called SharedPointer
. The specification for SharedPointer
is given below:
Method | Description | Usage | Exceptions |
---|---|---|---|
SharedPointer() |
A constructor which initialises a nullptr . |
|
None |
SharedPointer(int*) |
A constructor which initialises *this with ptr 's resource. |
|
None |
~SharedPointer() |
If *this is the last SharedPointer to the resource owned by *this , then the resource is destroyed. |
|
None |
int* get() |
Returns the stored pointer. | int* ptr1{new int{42}}; SharedPointer sp(ptr); int* ptr2{sp.get()}; |
None |
long use_count() |
Returns the number of SharedPointer managing *this 's resource. |
|
None |
int& operator*() |
Dereferences the stored pointer in *this . |
|
None |
Take the SharedPointer
class from lab03-1 and extend it with the following specification:
Method | Description | Usage | Exceptions |
---|---|---|---|
SharedPointer(SharePointer const& sp) |
Copy constructor. The use count should increase by 1. |
|
None |
SharedPointer(SharePointer&& sp) |
Move constructor. The use count should not increase. |
|
None |
SharedPointer& operator=(SharePointer const& sp) |
Copy assignment. The use count should increase by 1. |
|
None |
SharedPointer& operator=(SharePointer&& sp) |
Move assignment. The use count should not increase. |
|
None |
friend bool operator==(SharedPointer const& lhs, SharedPointer const& rhs) |
Returns true if the values of the resources owned by lhs and rhs are the same. |
|
None |
friend std::ostream& operator<<(std::ostream& os, SharedPointer const& sp) |
Outputs the value of the resource owned by *this . |
|
None |