#include <iostream>
using namespace std;
template<typename Type>
class auto_ptr_
{
public:
auto_ptr_(Type *t = NULL) :ptr(t), count(new int(1))
{
}
auto_ptr_(const auto_ptr_& at) :ptr(at.ptr), count(at.count)
{
count[0]++;
}
auto_ptr_& operator=(const auto_ptr_ &at)
{
if (this != &at)
{
if (ptr == at.ptr)return *this;
else
{
realse();
ptr = at.ptr;
count = at.count;
count[0]++;
}
}
return *this;
}
~auto_ptr_()
{
realse();
}
Type* operator->()
{
return ptr;
}
Type& operator*()
{
return *ptr;
}
private:
void realse()
{
if (--count[0] == 0)
{
cout << "free" << endl;
delete []count;
if (ptr != NULL)
{
delete[] ptr;
}
}
}
private:
int *count;
Type *ptr;
};
int main()
{
int *a = new int(1);
auto_ptr_<int> ps(a);
cout << *ps << endl;
return 0;
}