#include<iostream>
#include<memory>
using namespace std;
template<typename T>
class SmartPtr {
public:
SmartPtr(T *p = NULL) :_ptr(p) {
if (_ptr)_count = new size_t(1);
else
_count = new size_t(0);
}
SmartPtr(const SmartPtr& p) {
if (this != &p) {
this->_ptr = p._ptr;
this->_count = p._count;
(*this->_count)++;
}
}
SmartPtr& operator=(const SmartPtr& p)
{
++*p._count;
if (--*_count == 0) {
delete _count;
delete _ptr;
}
this->_ptr = p._ptr;
this->_count = p._count;
return *this;
}
T &operator*() {
return *(this->_ptr);
}
T *operator->() {
return this->_ptr;
}
~SmartPtr()
{
if (--*_count == 0) {
delete _ptr;
delete _count;
}
}
size_t use_count()
{
return *this->_count;
}
private:
T* _ptr;
size_t *_count;
};