利用模板实现c++智能指针
#include "std_lib_facilities.h"
using namespace std;
class Point//used for test
{
public:
Point(double xval,double yval):x(xval),y(yval)
{
}
void set_x(double xval)
{
x=xval;
}
void set_y(double yval)
{
y=yval;
}
double get_x()
{
return x;
}
double get_y()
{
return y;
}
private:
double x;
double y;
};
template<typename FriendClass,typename DataType>
class U_Ptr
{
friend FriendClass;
U_Ptr(DataType *p):tp(p),count(1)
{
cout <<"U_Ptr constructor called"<<endl;
}
~U_Ptr()
{
cout <<"U_Ptr distructor called!"<<endl;
delete tp;
}
DataType *tp;
int count;
};
template<typename DataType>
class Counted_ptr
{
public:
Counted_ptr(DataType *p):u_ptr(new U_Ptr<Counted_ptr,DataType>(p))
{
cout <<"Counted_ptr constructor called!"<< " count=" << u_ptr->count << endl;
}
Counted_ptr(const Counted_ptr &temp):u_ptr(temp.u_ptr)
{
++u_ptr->count;
cout << "Counted_ptr copy constructor called" <<" count=" << u_ptr->count << endl;
}
DataType& operator *()
{
return *(u_ptr->tp);
}
DataType* operator ->()
{
return u_ptr->tp;
}
Counted_ptr& operator =(const Counted_ptr& father)
{
++father.u_ptr->count;
u_ptr->count=u_ptr->count-1;
if(u_ptr->count==0)
delete u_ptr;
u_ptr=father.u_ptr;
return *this;
}
~Counted_ptr()
{
cout << "Counted_ptr distructor called!" << " count=" << u_ptr->count <<endl;
if(--u_ptr->count==0)
delete u_ptr;
}
private:
U_Ptr<Counted_ptr,DataType> *u_ptr;
};
int main()
{
int test=0;
Point *p=new Point(1,1);
Point *q=new Point(2,2);
Counted_ptr<Point> cptr_1(p);
Counted_ptr<Point> cptr_2(q);
cout << cptr_1->get_x() << " " << cptr_1->get_y() << endl;
cout << cptr_2->get_x() << " " << cptr_2->get_y() << endl;
Counted_ptr<Point> cptr_3(cptr_1);
Counted_ptr<Point> cptr_4(cptr_1);
Counted_ptr<Point> cptr_5(cptr_3);
cptr_1=cptr_3;
cptr_1=cptr_4;
cptr_1=cptr_5;
cptr_1=cptr_2;
cin >> test;
return 0;
}
using namespace std;
class Point//used for test
{
public:
Point(double xval,double yval):x(xval),y(yval)
{
}
void set_x(double xval)
{
x=xval;
}
void set_y(double yval)
{
y=yval;
}
double get_x()
{
return x;
}
double get_y()
{
return y;
}
private:
double x;
double y;
};
template<typename FriendClass,typename DataType>
class U_Ptr
{
friend FriendClass;
U_Ptr(DataType *p):tp(p),count(1)
{
cout <<"U_Ptr constructor called"<<endl;
}
~U_Ptr()
{
cout <<"U_Ptr distructor called!"<<endl;
delete tp;
}
DataType *tp;
int count;
};
template<typename DataType>
class Counted_ptr
{
public:
Counted_ptr(DataType *p):u_ptr(new U_Ptr<Counted_ptr,DataType>(p))
{
cout <<"Counted_ptr constructor called!"<< " count=" << u_ptr->count << endl;
}
Counted_ptr(const Counted_ptr &temp):u_ptr(temp.u_ptr)
{
++u_ptr->count;
cout << "Counted_ptr copy constructor called" <<" count=" << u_ptr->count << endl;
}
DataType& operator *()
{
return *(u_ptr->tp);
}
DataType* operator ->()
{
return u_ptr->tp;
}
Counted_ptr& operator =(const Counted_ptr& father)
{
++father.u_ptr->count;
u_ptr->count=u_ptr->count-1;
if(u_ptr->count==0)
delete u_ptr;
u_ptr=father.u_ptr;
return *this;
}
~Counted_ptr()
{
cout << "Counted_ptr distructor called!" << " count=" << u_ptr->count <<endl;
if(--u_ptr->count==0)
delete u_ptr;
}
private:
U_Ptr<Counted_ptr,DataType> *u_ptr;
};
int main()
{
int test=0;
Point *p=new Point(1,1);
Point *q=new Point(2,2);
Counted_ptr<Point> cptr_1(p);
Counted_ptr<Point> cptr_2(q);
cout << cptr_1->get_x() << " " << cptr_1->get_y() << endl;
cout << cptr_2->get_x() << " " << cptr_2->get_y() << endl;
Counted_ptr<Point> cptr_3(cptr_1);
Counted_ptr<Point> cptr_4(cptr_1);
Counted_ptr<Point> cptr_5(cptr_3);
cptr_1=cptr_3;
cptr_1=cptr_4;
cptr_1=cptr_5;
cptr_1=cptr_2;
cin >> test;
return 0;
}