#ifndef SMART_PTR_H
#define SMART_PTR_H
#include<cstddef>
template <typename T> class Smart_ptr
{
public:
class U_ptr
{friend class Smart_ptr;
U_ptr(T *p):tp(p),use(1){}
~U_ptr(){delete tp;}
T *tp;
size_t use;
};
public:
Smart_ptr(T *tp):ptr(new U_ptr(tp)){}
~Smart_ptr(){if(--ptr->use==0) delete ptr;}
Smart_ptr(const Smart_ptr& other):ptr(other.ptr){++ptr->use;}
Smart_ptr& operator=(const Smart_ptr& rhs)
{ ++rhs.ptr->use;
if(--ptr->use == 0) delete ptr;
ptr=rhs.ptr;
return *this;
}
T & operator*(){return *ptr->tp;}
T * operator->(){return ptr->tp;}
const T & operator*() const {return *ptr->tp;}
const T * operator->() const {return ptr->tp;}
size_t use_count(){return ptr->use;}
private:
U_ptr *ptr;
};
#endif // SMART_PTR_H
#define SMART_PTR_H
#include<cstddef>
template <typename T> class Smart_ptr
{
public:
class U_ptr
{friend class Smart_ptr;
U_ptr(T *p):tp(p),use(1){}
~U_ptr(){delete tp;}
T *tp;
size_t use;
};
public:
Smart_ptr(T *tp):ptr(new U_ptr(tp)){}
~Smart_ptr(){if(--ptr->use==0) delete ptr;}
Smart_ptr(const Smart_ptr& other):ptr(other.ptr){++ptr->use;}
Smart_ptr& operator=(const Smart_ptr& rhs)
{ ++rhs.ptr->use;
if(--ptr->use == 0) delete ptr;
ptr=rhs.ptr;
return *this;
}
T & operator*(){return *ptr->tp;}
T * operator->(){return ptr->tp;}
const T & operator*() const {return *ptr->tp;}
const T * operator->() const {return ptr->tp;}
size_t use_count(){return ptr->use;}
private:
U_ptr *ptr;
};
#endif // SMART_PTR_H