#include
#include
using Poco::AutoPtr;
class object{
public:
object(int a):data(a),count(1){
}
~object(){}
void printData(){
std::cout<<"data->"<
std::cout<<"count->"<
}
public:
void duplicate(){
++count;
}
void release(){
--count;
if(count == 0){
delete this;
}
}
private:
int data;
int count;
};
int main(int argc,char** argv){
AutoPtr
ap->printData();
AutoPtr
ap1->printData();
return 0;
}
关键点总结:
1.被AutoPtr管理的类必须维持一个引用计数,这里定义为count,在调用构造函数时把该引用计数初始化为1
2.被AutoPtr管理的类必须定义void duplicate()方法,该方法使引用计数count增1
3.被AutoPtr管理的类必须定义void release()方法,该方法使引用计数count减1,并且当引用计数为0时,删除该对象
PS:初写文章,文笔生涩之处,各位请见谅,若有疑问或者交流的,可加本人YY号:301558660
转载请注明出处:山水间博客, http://blog.csdn.net/linyanwen99/article/details/8052689