Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3446501
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: C/C++

2010-03-02 16:25:26

 为了防止程序出现异常,没有及时delete相应的资源,C++出现了auto_ptr类。本程序是一个模拟
智能指针的程序。指针指向一个自定义类型String,使用这个指针类SmartPtr,和平时的指针String*
一样。而它的优势是,程序结束前,不必显示的去调用delete,释放掉分配的内存。在Dev C++环境中,
显示输出的析构测试语句可能不会输出显示,图中显示为VC++ 6.0的结果。
                      
#include
#include
using namespace std;

class String
{
private:
    char* str;
public:
    String()
    {
        str=NULL;
    }
    String(const char* s);
    String(const String &sObj);
    ~String();
    String &operator=(const String &sObj);
    void show();
};

String::String(const char* s)
{
    assert(s!=NULL);
    str=new char[strlen(s)+1];
    if(str==NULL)
    {
        cout << "Allocation Failure!" << endl;
        exit (1);
    }
    strcpy(str,s);
}

String::String(const String &sObj)
{
    assert(sObj.str!=NULL);
    str=new char[strlen(sObj.str)+1];
    if(str==NULL)
    {
        cout << "Allocation Failure!" << endl;
        exit (1);
    }
    strcpy(str,sObj.str);
}

String::~String()
{
    cout << "String::destructor..." << endl;
    if(str!=NULL)
    {
       delete []str;
       str=NULL;
    }
}

String& String::operator=(const String &sObj)
{
    if(this == &sObj)
       return *this;
    delete []str;
    str=NULL;
    str=new char[strlen(sObj.str)+1];
    if(str==NULL)
    {
        cout << "Allocation Failure!" << endl;
        exit (1);
    }
    strcpy(str,sObj.str);
    return *this;
}

void String::show()
{
    cout << str << endl;
}

template
class SmartPtr
{
private:
    Telem* ptr;
    size_t* pRefTimes;
    void decRefTimes()
    {
       (*pRefTimes)--;
       if(*pRefTimes==0)
       {
          delete ptr;
          delete pRefTimes;
       }
    }
public:
    SmartPtr(Telem* p=NULL);
    ~SmartPtr();
    SmartPtr(const SmartPtr& sp);
    SmartPtr& operator=(const SmartPtr& sp);
    Telem* operator->();
    Telem& operator*();
    void pRefTimesShow();
};

template
SmartPtr::SmartPtr(Telem* p)
{
    ptr=p;
    pRefTimes=new size_t(1);
}

template
SmartPtr::~SmartPtr()
{
    cout << "SmartPtr::destructor..." << endl;
    decRefTimes();
}

template
SmartPtr::SmartPtr(const SmartPtr& sp)
{
    ptr=sp.ptr;
    (*sp.pRefTimes)++;
    pRefTimes=sp.pRefTimes;
}


template
SmartPtr& SmartPtr::operator=(const SmartPtr& sp)
{
    ++(*sp.pRefTimes);
    decRefTimes();
    ptr=sp.ptr;
    pRefTimes=sp.pRefTimes;
    return *this;
}

template
Telem* SmartPtr::operator->()
{
    if(ptr)
       return ptr;
}

template
Telem& SmartPtr::operator*()
{
    if(ptr)
       return *ptr;
}

template
void SmartPtr::pRefTimesShow()
{
    cout << *pRefTimes << endl;
}

int main()
{
    String* s=new String("Hello World!");
    SmartPtr sp1(s);
    sp1->show();
    SmartPtr sp2(sp1);
    sp2->show();
    SmartPtr sp3(new String("haha"));
    sp3=sp2;
    sp3.pRefTimesShow();
    //system("pause");
    return 0;
}
阅读(805) | 评论(0) | 转发(0) |
0

上一篇:智能指针

下一篇:学用PowerPoint快捷键

给主人留下些什么吧!~~