template<typename T>
class my_shared_ptr {
public:
my_shared_ptr() {
ptr_ = new T;
count_ = new int;
*count_ = 1;
}
my_shared_ptr(const my_shared_ptr& in) {
if (ptr_) Decrease();
ptr_ = in.ptr_;
count_ = in.count_;
Increase();
}
my_shared_ptr& operator=(const my_shared_ptr& in) {
if (ptr_) Decrease();
ptr_ = in.ptr_;
count_ = in.count_;
Increase();
return *this;
}
~my_shared_ptr() {
Decrease();
}
T operator*() {
return *ptr_;
}
T* operator->() {
return ptr_;
}
private:
void Increase() {
if(count_) ++(*count_);
}
void Decrease() {
if (count_ && --(*count_) == 0) {
delete ptr_;
delete count_;
ptr_ = nullptr;
count_ = nullptr;
}
}
private:
T* ptr_ = nullptr;
int* count_ = nullptr;
};