自己实现的智能指针:
优点:有引用计数,可以实现指针间共享一个对象。
缺点:不能实现数组的动态分配。
auto_cptr.h:
#ifndef _AUTO_CPTR_H_
#define _AUTO_CPTR_H_
//#define __debug
// auto_cptr.h
// 带引用计数的智能指针
#include <iostream>
using namespace std;
template <typename T>
class auto_cptr
{
public:
auto_cptr():p(0),count(0)
{
#ifdef __debug
cout<<"auto_cptr():empty construction of auto_cptr"<<endl;
#endif
}
template <typename U>
explicit auto_cptr(U *pu):p( (T*)pu ),count(0)
{
if(pu)
{
count=new size_t;
*count = 1;
#ifdef __debug
cout<<"auto_cptr(U *pu): construction of auto_cptr, obj="<<p<<", count="<<get_count()<<" "<<endl;
#endif
}else
{
#ifdef __debug
cout<<"auto_cptr(U *pu): construction of auto_cptr, null=null"<<endl;
#endif
}
}
auto_cptr(const auto_cptr<T>& cp):p( 0 ),count(0)
{
copyfrom(cp);
#ifdef __debug
cout<<"auto_cptr(const auto_cptr<T>& cp):copy construction of auto_cptr.obj="
<<p<<", count="<<get_count()<<" "<<endl;
#endif
}
auto_cptr<T>& operator=(const auto_cptr <T>&cp)
{
if(&cp==this)
return *this;
copyfrom(cp);
#ifdef __debug
cout<<"auto_cptr::operator=:operator"<<"obj="<<p<<", count="<<get_count()<<" "<<endl;
#endif
return *this;
}
~auto_cptr()
{
#ifdef __debug
cout<<"~auto_cptr()"<<endl;
#endif
minus();
}
friend bool operator==(const auto_cptr<T>&p1,const auto_cptr<T>&p2)
{
return p1.p==p2.p && p1.count==p2.count;
}
friend bool operator!=(const auto_cptr<T>&p1,const auto_cptr<T>&p2)
{
return !(p1==p2);
}
T *operator->() {return &(operator*());}
T &operator*() {return *p;}
size_t get_count()
{
return count?*count:0;
}
T* get_pvalue()
{
return p;
}
private:
void copyfrom(const auto_cptr<T> &cp)
{
minus();
if(cp.p)
{
p=cp.p;
count=cp.count;
++(*count);
}
}
void minus()
{
if(p==0)
return;
--(*count);
if(*count<=0)
{
#ifdef __debug
cout<<"auto_cptr::minus(): delete "<<endl;
#endif
delete p;
delete count;
}else
{
#ifdef __debug
cout<<"auto_cptr::minus():not delete, p="<<p<<", count="<<get_count()<<" "<<endl;
#endif
}
p=0;
count=0;
}
private:
T *p;
size_t *count;
};
#endif
测试:main.c:
#include <iostream>
#define __debug // 打印智能指针的调试信息
#include "auto_cptr.h"
using namespace std;
// 测试类
class A
{
public:
A(){cout<<"empty construction"<<endl;}
A(int a_){cout<<"non-empty construction"<<endl;_a=a_;}
~A(){cout<<"deconstruction"<<endl;}
void display(){cout<<"A::display()"<<endl;}
friend ostream &operator<<(ostream &os, const A&a);
private:
int _a;
};
ostream &operator<<(ostream &os, const A&a)
{
cout<<a._a;
return os;
}
int main()
{
auto_cptr<A> p1=auto_cptr<A> (new A(5) );
auto_cptr<A> p2=p1; // 拷贝构造
auto_cptr<A>p3; // 空的智能指针
p3=p2; // 智能指针的赋值
p2=auto_cptr<A>(); // 空对象赋值给p2
cout<<"指针指向的对象值: "<<*p3<<endl; // operator *
cout<<"通过智能指针调用函数: "; p3->display(); // operator ->
return 0;
}
运行:
chen@chen-book1:~$ gcc c.cpp -lstdc++ -o cpp -g
chen@chen-book1:~$ ./cpp
non-empty construction
auto_cptr(U *pu): construction of auto_cptr, obj=0x9c3c008, count=1
auto_cptr(const auto_cptr<T>& cp):copy construction of auto_cptr.obj=0x9c3c008, count=2
auto_cptr():empty construction of auto_cptr
auto_cptr::operator=:operatorobj=0x9c3c008, count=3
auto_cptr():empty construction of auto_cptr
auto_cptr::minus():not delete, p=0x9c3c008, count=2
auto_cptr::operator=:operatorobj=0, count=0
~auto_cptr()
指针指向的对象值: 5
通过智能指针调用函数: A::display()
~auto_cptr()
auto_cptr::minus():not delete, p=0x9c3c008, count=1
~auto_cptr()
~auto_cptr()
auto_cptr::minus(): delete
deconstruction
chen@chen-book1:~$
422

被折叠的 条评论
为什么被折叠?



