??????????????????????
????????????????????????????????????????????????
int main()
{
//????????????????????
Point *pa = new Point(10?? 20);
//???????????????????????????????????pa
//?????????????????????????????????????仯
{
SmartPtr sptr1(pa);//???????count=1
{
SmartPtr sptr2(sptr1); //??????????????????????count=2
{
SmartPtr sptr3=sptr1; //??????????????????????conut=3
}
//???count=2
}
//???count=1??
}
//???count=0??pa????delete??
cout << pa->getX ()<< endl;
system("pause");
return 0;
}
?????????????н??????
????????2???????????????
????????1???????????????
????-17891602
???????????????. . .
????????????????????????????????????3->2->1->0?任????????0???pa????delete????????getX??????????????????????
?????????????????
????????????SmartPtr????????????????????????????????????????->??*??????????????????????????????????????????????Щ?????????????????????
{
public:
SmartPtr(Point *ptr) :rp(new RefPtr(ptr)) { }
SmartPtr(const SmartPtr &sp) :rp(sp.rp) { ++rp->count; }
SmartPtr& operator=(const SmartPtr& rhs) {
++rhs.rp->count;
if (--rp->count == 0)
delete rp;
rp = rhs.rp;
return *this;
}
~SmartPtr() {
if (--rp->count == 0)
delete rp;
else
cout << "????" << rp->count << "???????????????" << endl;
}
Point & operator *()        //????*??????
{
return *(rp->p);
}
Point* operator ->()       //????->??????
{
return rp->p;
}
private:
RefPtr *rp;
};
????????????????????????????????
????Point *pa = new Point(10?? 20);
????SmartPtr sptr1(pa);
????//?????????
????cout<<sptr1->getX();
???????????????
???????????????????????????Point?????????????????????????????????????????????????д??????????????????????????????????????????????????Point?????????????????????????????????????弼????????????????????????????????????????????б???????????????????????????????????????????????????棺
//?????????????????????
template <typename T>
class SmartPtr;
template <typename T>
class U_Ptr     //??????
{
private:
//?????????????????private???????????????????????
friend class SmartPtr<T>;      //?????????????????????????????????????????????
//????????????????????????
U_Ptr(T *ptr) :p(ptr)?? count(1) { }
//????????
~U_Ptr() { delete p; }
//???ü???
int count;
//???????????
T *p;
};
template <typename T>
class SmartPtr   //?????????
{
public:
SmartPtr(T *ptr) :rp(new U_Ptr<T>(ptr)) { }      //??????
SmartPtr(const SmartPtr<T> &sp) :rp(sp.rp) { ++rp->count; }  //?????????
SmartPtr& operator=(const SmartPtr<T>& rhs) {    //????????????
++rhs.rp->count;     //???????????????ü?????1??
if (--rp->count == 0)     //??????ü?????1?????????????
delete rp;
rp = rhs.rp;
return *this;
}
T & operator *()        //????*??????
{
return *(rp->p);
}
T* operator ->()       //????->??????
{
return rp->p;
}
~SmartPtr() {        //????????
if (--rp->count == 0)    //?????ü??????0????????????????????????????????
delete rp;
else
cout << "????" << rp->count << "???????????????" << endl;
}
private:
U_Ptr<T> *rp;  //????????????
};
?????????????????????????????????????????????????????????????????????int??
int main()
{
int *i = new int(2);
{
SmartPtr<int> ptr1(i);
{
SmartPtr<int> ptr2(ptr1);
{
SmartPtr<int> ptr3 = ptr2;
cout << *ptr1 << endl;
*ptr1 = 20;
cout << *ptr2 << endl;
}
}
}
system("pause");
return 0;
}
???????н???????????SmartPtr???????int?????????
????2
????20
????????2???????????????
????????1???????????????
???????????????. . .