Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4559
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-04 20:16
文章分类

全部博文(2)

文章存档

2015年(2)

我的朋友
最近访客

分类: C/C++

2015-07-13 10:34:53

//智能指针完整程序
#include<iostream>
using namespace std;
class auto_p
{
    class node
    {
        public:
            int count = 0;
            int *head = nullptr;
    };
    node *p = nullptr;
    public:
    auto_p() = default;
    auto_p(int *data)
    {
        if(p)
        {
            p->count --;
            if(0 ==p->count)
            {
                delete p->head;
                delete p;
              }
         }
         else
         {
                p = new node();
                p->head = data;
                p->count ++;                
          }
    }
    auto_p(const auto_p &ap)
    {
        if(p)
          {
                p->count--;
                if(0 ==p->count)
                {
                    delete p->head;
                    delete p;
                }
                else
                {
                    p = ap.p;
                    if(p)
                     {
                        p->count++;
                        }
                 }
            }
            else
            {
                p = ap.p;
                   if(p)
                    {
                        p->count++;
                    }
             }
     }
        ~auto_p()
        {
            if(p)
            {
                p->count--;
                   if(0 == p->count)
                    {
                        delete p->head;
                        delete p;
                    }
            }
        }
}
阅读(221) | 评论(0) | 转发(0) |
0

上一篇:不同机器各种类型占用字节大小

下一篇:没有了

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