码迷,mamicode.com
首页 > 编程语言 > 详细

C++智能指针大总结

时间:2015-07-11 12:15:23      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:智能指针


//最挫的智能指针。
#include<iostream>
using namespace std;
template<typename Type>
class my_auto_ptr
{
public:
    my_auto_ptr(Type* p = NULL) :ptr(p){}
    my_auto_ptr(const my_auto_ptr& ma)
    {
        ptr = ma.realse();
    }
    my_auto_ptr& operator=(const my_auto_ptr& ma)
    {
        if (this != &ma)
        {
            this->realse();
            ptr = ma.realse();
        }
        return *this;
    }
    ~my_auto_ptr()
    {
        if (ptr != NULL)
        {
            cout << "free" << endl;
            delete ptr;
            ptr = NULL; 
        }
    }
    Type* realse()const
    {
        Type* temp = ptr;
        (Type*)ptr = NULL;
        return temp;
    }
    Type operator*()
    {
        return *ptr;
    }
    Type* operator->()
    {
        return ptr;
    }
private:
    Type *ptr;
};
int main()
{
    my_auto_ptr<int> ps(new int(1000));
    cout << *ps << endl;
    my_auto_ptr<int> pb;
        pb = ps;
    //pb = ps;
    return 0;
}


#include <iostream>
using namespace std;
//vs2013里面的智能指针。
template<typename Type>
class my_auto_ptr
{
public:
    my_auto_ptr(Type *p = NULL):ptr(p), owns(p!=NULL){}
    my_auto_ptr(const my_auto_ptr &ma)
    {
        ptr = ma.relase();
    }
    my_auto_ptr& operator=(const my_auto_ptr &ma)
    {
        if (this != &ma)
        {
            this->relase();
            ptr = ma.relase();
            owns = true;
        }
        return *this;
    }
    Type* relase()const 
    {
        ((my_auto_ptr*)this)->owns = false;
        return ptr;
    }
    ~my_auto_ptr()
    {
        if (owns)
        {
            cout << "free" << endl;
            delete ptr;
            ptr = NULL;
        }
    }
    Type operator*()
    {
        return *ptr;
    }
    Type* operator->()
    {
        return ptr;
    }
private:
    Type *ptr;
    bool owns;
};
int main()
{
    my_auto_ptr<int> ps(new int(10));
    my_auto_ptr<int> pb;
    pb = ps;
    //cout << *ps << endl;
    return 0;
}


#include <iostream>
using namespace std;
//唯一对象智能指针。
template<typename Type>
class my_auto_ptr
{
public:
    my_auto_ptr(Type *p = NULL) :ptr(p){}
    ~my_auto_ptr()
    {
        if (ptr != NULL)
        {
            delete ptr;
            ptr = NULL;
        }
    }
    Type& operator*()
    {
        return *ptr;
    }
    Type* operator->()
    {
        return ptr;
    }
protected:
    my_auto_ptr& operator=(const my_auto_ptr& ma);
    my_auto_ptr(const my_auto_ptr& ma);
private:
    Type *ptr;
};
int main()
{
    my_auto_ptr<int> ps(new int(3));
    cout << *ps << endl;
    return 0;
}

#include <iostream>
using namespace std;
//引用计数的智能指针。
template<typename Type>
class my_auto_ptr
{
public:
    my_auto_ptr(Type *p = NULL) :ptr(p)
    {
        count = new int[1];
        count[0] = 1;
    }
    my_auto_ptr(const my_auto_ptr& ma)
    {
        count = ma.count;
        count[0]++;
        ptr = ma.ptr;
    }
    my_auto_ptr& operator=(const my_auto_ptr& ma)
    {
        if (this != &ma)
        {
            this->~my_auto_ptr();
            count = ma.count;
            count[0]++;
            ptr = ma.ptr;
        }
        return *this;
    }
    ~my_auto_ptr()
    {
        if (count!=NULL && count[0]-- == 1)
        {
            cout << "free" << endl;
            delete ptr;
            ptr = NULL;
            delete[] count;
            count = NULL;
        }
    }
    Type operator*()
    {
        reutrn *ptr;
    }
    Type* operator->()
    {
        return ptr;
    }
private:
    Type* ptr;
    int *count;
};
int main()
{
    my_auto_ptr<int> ps(new int(100));
    my_auto_ptr<int> pb(ps);
    my_auto_ptr<int> pd(new int(200));
    pd = pb;
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++智能指针大总结

标签:智能指针

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/46840127

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!