????????C++?????????????????????????????????ù????С?????????????????new??malloc?????????????????????????????????????й???
????????????????????????????????????
???????????smart pointer????????????ü????????????????????????????????????
???????????£?????????????????????????????????????????
????????????????洢???????????????????????ü?????????????????????????????????????????????й???
???????????????????????????????????ü??????????????????ü?????????????????????????????
??????δ???????μ????????????????????????ü??????1????????????????????????????????????????????????????????????????????????ü??????????????????и????????????????????????????????????ü????????????ü????????0??????????????????????????????????????ü?????????????????????????????????ü????????????ü???????0???????????????
???????μ?????????????????????????????????
#include<iostream>
#include<stdexcept>
using namespace std;
#define TEST_SMARTPTR
class Stub
{
public:
void print(){
cout<<"Stub:print"<<endl;
}
~Stub(){
cout<<"Stub:Destructor"<<endl;
}
};
template<typename T>
class SmartPtr
{
public:
SmartPtr(T*p=0):ptr(p)??pUse(new size_t(1)){}
SmartPtr(const SmartPtr& src):ptr(src.ptr)??pUse(src.pUse){
++*pUse;
}
SmartPtr&operator=(const SmartPtr& rhs){
//self-assigningisalsoright
++*rhs.pUse;
decrUse();
ptr=rhs.ptr;
pUse=rhs.pUse;
return *this;
}
T* operator->(){
if(ptr)
return ptr;
throw std::runtime_error("accessthroughNULLpointer");
}
const T* operator->()const{
if(ptr)
return ptr;
throw std::runtime_error("accessthroughNULLpointer");
}
T &operator*(){
if(ptr)
return *ptr;
throw std::runtime_error("dereferenceofNULLpointer");
}
const T &operator*()const{
if(ptr)
return *ptr;
throw std::runtime_error("dereferenceofNULLpointer");
}
~SmartPtr(){
decrUse();
#ifdef TEST_SMARTPTR
std::cout<<"SmartPtr:Destructor"<<std::endl;//fortesting
#endif
}
private:
void decrUse(){
if(--*pUse==0){
delete ptr;
delete pUse;
}
}
T* ptr;
size_t* pUse;
};
int main()
{
try{
SmartPtr<Stub> t;
t->print();
}catch(const exception&err){
cout<<err.what()<<endl;
}
SmartPtr<Stub>t1(new Stub);
SmartPtr<Stub>t2(t1);
SmartPtr<Stub>t3(new Stub);
t3=t2;
t1->print();
(*t3).print();
return 0;
}
?????????????У??????????????????->??*???????????????????????????????????????????????????
????????????????????????????ü??????????????????????????????????????????????????????????????????????????й???????