Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- class my_shared_ptr
- {
- int* count;
- T* ptr;
- public:
- my_shared_ptr();
- my_shared_ptr(T* ptr);
- my_shared_ptr(const my_shared_ptr& obj);
- my_shared_ptr(my_shared_ptr&& obj);
- ~my_shared_ptr();
- T* get()const;
- int use_count()const;
- bool unique()const;
- T& operator=(const my_shared_ptr& obj);
- T& operator*()const;
- T* operator->() const;
- T& operator[](int index)const;
- bool operator==(nullptr_t)const;
- bool operator==(const my_shared_ptr& obj)const;
- bool operator!=(const my_shared_ptr& obj)const;
- bool operator<(const my_shared_ptr& obj)const;
- bool operator<=(const my_shared_ptr& obj)const;
- bool operator>(const my_shared_ptr& obj)const;
- bool operator>=(const my_shared_ptr& obj)const;
- friend ostream& operator<<<>(ostream& os, my_shared_ptr& obj);
- };
- template <class T>
- my_shared_ptr<T>::my_shared_ptr()
- {
- ptr = nullptr;
- count = nullptr;
- }
- template <class T>
- my_shared_ptr<T>::my_shared_ptr(T* ptr)
- {
- count = new int{ 1 };
- this->ptr = ptr;
- }
- template <class T>
- my_shared_ptr<T>::my_shared_ptr(const my_shared_ptr& obj)
- {
- count = obj.count;
- (*count)++;
- ptr = obj.ptr;
- }
- template <class T>
- my_shared_ptr<T>::my_shared_ptr(my_shared_ptr&& obj)
- {
- count = obj.count;
- obj.count == nullptr;
- ptr = obj.ptr;
- obj.ptr == nullptr;
- }
- template<class T>
- my_shared_ptr<T>::~my_shared_ptr()
- {
- if (ptr != nullptr)
- {
- if (*count == 1)
- {
- delete[] ptr;
- delete count;
- }
- else (*count)--;
- }
- }
- template <class T>
- T* my_shared_ptr<T>::get()const
- {
- return ptr;
- }
- template <class T>
- int my_shared_ptr<T>::use_count()const
- {
- return *count;
- }
- template <class T>
- bool my_shared_ptr<T>::unique()const
- {
- return *count == 1;
- }
- template<class T>
- inline T& my_shared_ptr<T>::operator=(const my_shared_ptr& obj)
- {
- if (this == &obj) return *this;
- count = obj.count;
- (*count)++;
- ptr = obj.ptr;
- return *this;
- }
- template <class T>
- T& my_shared_ptr<T>::operator*()const
- {
- return *ptr;
- }
- template <class T>
- T* my_shared_ptr<T>::operator->()const
- {
- return *ptr;
- }
- template <class T>
- T& my_shared_ptr<T>::operator[](int index)const
- {
- return ptr[index];
- }
- template <class T>
- bool my_shared_ptr<T>::operator==(nullptr_t)const
- {
- return ptr == nullptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator==(const my_shared_ptr& obj)const
- {
- return ptr == obj.ptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator!=(const my_shared_ptr& obj)const
- {
- return ptr != obj.ptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator<(const my_shared_ptr& obj)const
- {
- return ptr < obj.ptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator<=(const my_shared_ptr& obj)const
- {
- return ptr <= obj.ptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator>(const my_shared_ptr& obj)const
- {
- return ptr > obj.ptr;
- }
- template <class T>
- bool my_shared_ptr<T>::operator>=(const my_shared_ptr& obj)const
- {
- return ptr >= obj.ptr;
- }
- template <class T>
- ostream& operator<<<>(ostream& os, my_shared_ptr<T>& obj)
- {
- os << *(obj.ptr);
- return os;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement