C++ ???ü?????????????????????
???????????? ???????[ 2015/9/25 14:06:07 ] ??????????????????? .NET
??????????????????????
????????????????????????????????????????????????
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???????????????
???????????????. . .
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
??C++????????????C++ lvalue??rvalueC++11????????C++???????????????C++?е?????????????????C++?????????C++???Windows????λ??C/C++???????????????????JAVA??C??C++??????????c++??python???????????????????????????????C++???????C++?е????????C++????????????????C++ ???????????????C++?????????????????????C++????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????