C++???????????????
???????????? ???????[ 2014/3/14 11:16:06 ] ????????C++ ???
??????SmartPointer???У?????_counter?????
template<classT>
classSmartPointer{
public:
SmartPointer(T*t):pc(new_counter(1)){
cout<<"SmartPointer::SmartPointer()invodeduseis:"<<pc->use<<endl;
this->pt=t;
}
SmartPointer(SmartPointer<T>&rhs){
this->pc=rhs.pc;
this->pt=rhs.pt;
this->pc->use++;
cout<<"SmartPointercopyinvokeduseis:"<<pc->use<<endl;
}
~SmartPointer(){
pc->use--;
cout<<"SmartPointer::~SmartPointer()invodeduseis:"<<pc->use<<endl;
if(pc->use==0)
{
deletept;
deletepc;
}
}
SmartPointer<T>&operator=(SmartPointer<T>rhs){
if(rhs==*this){
return*this;
}
this->pt=rhs.pt;
this->pc=rhs.pc;
this->pc->use++;
cout<<"SmartPointer::operator=()invokeduseis:"<<pc->use<<endl;
return*this;
}
private:
T*pt;
_counter*pc;
};
???????磺?????????HasPtr???????????????????*p??
classHasPtr{
public:
HasPtr(intval):value(val)??p(newint(3)){
cout<<"HasPtr::HasPtr()invoked"<<endl;
}
~HasPtr(){deletep;cout<<"HasPtr::~HasPtr()invoded"<<endl;}
private:
int*p;
intvalue;
};
??????????μ????
????HasPtr*php=newHasPtr(3);
????SmartPointer<HasPtr>psp(php);
????SmartPointer<HasPtr>npsp(psp);
???????????????????????????????????HasPtr????????????£?
????_counter??use????????ü??????2.
??????
???·???
??????????????????
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