一步一步 实现智能指针(三)

来源:互联网 发布:javascript 表格并排放 编辑:程序博客网 时间:2024/05/17 22:41

//再依据 封装等。。将他代码,整理优化

#include <iostream>

using namespace::std;


/**

 *  计数器类。

 *  对一个地址的引用计数

 *  需要单独封装成一个类,这样才能把它作为 MySmartPoint的一个指针成员,才能保证,所有 同一个 对象用同一个计数器  具体演变过程看那前面的文章

 */

class Counter

{

private:

    int count;

    template <class T> friend class  MySmartPoint;

};


/**

 *  智能指针类,对指针进行管理

 *  保证了 new delete 对称

 *  考虑了 赋值,拷贝,

 */

template <class T>

class MySmartPoint

{

private:

    T *ptr;

    Counter *countPtr; //记录ptr 个数

    

public:

    //    MySmartPoint(){};//这个应收是没有意义的 构造方法

    MySmartPoint(T *p)

    {

        countPtr = new Counter();

        countPtr -> count =1;

        cout<< "count =" << countPtr -> count <<endl;

        this->ptr = p;

    };

    

    MySmartPoint(const MySmartPoint & sp)

    {

        sp.countPtr->count ++;

        countPtr = sp.countPtr;

        ptr = sp.ptr;

        cout<< "count =" << countPtr -> count <<endl;

        std::cout << "const 拷贝构造" << std::endl;

    }

    

    MySmartPoint(MySmartPoint &sp)

    {

        sp.countPtr->count ++;

        countPtr = sp.countPtr;

        ptr = sp.ptr;

        

        cout<< "count =" << sp.countPtr -> count <<endl;

        std::cout << 拷贝构造" << std::endl;

    };

    

    

    

    ~MySmartPoint()

    {

        countPtr -> count --;

        cout<<"析构 count= "<<countPtr -> count <<endl;

        if (0 ==  countPtr -> count)

        {

            delete  this->ptr;

            cout<< "delete point is "<<this->ptr<<endl;

        }

    }

    

    MySmartPointoperator=(const MySmartPoint &p)

    {

        if (this == &p)

        {

            return *this;

        }

        p.countPtr->count ++;

        

        countPtr->count --;

        if (countPtr->count == 0)

        {

            delete  this->ptr;

            cout<< "delete point is "<<this->ptr<<endl;

        }

        

        countPtr = p.countPtr;

        ptr = p.ptr;

        

        std::cout << " operator=" << std::endl;

        return *this;

    }

    

    T* operator->()

    {

        return ptr;

    }

    const T* operator->()const

    {

        return ptr;

    }

    T &operator*()

    {

        return *ptr;

    }

    const T &operator*()const

    {

        return *ptr;

    }    

};




class Point

{

private:

    int x;

    int y;

public:

    Point(int xVal,int yVal):x(xVal),y(yVal)

    {

        std::cout<<"creat point adress is "<<this <<std::endl;

    }

    ~Point()

    {

        std::cout<<"relase point adress is "<<this <<std::endl;

    }

    

    int getX()

    {

        return x;

    };

    int getY()

    {

        return y;

    }

};

int main()

{

    Point *p = new Point(3,4);

    MySmartPoint<Point> sp = MySmartPoint<Point>(p);

    MySmartPoint<Point> sp1 = MySmartPoint<Point>(sp);

    MySmartPoint<Point> sp2 = sp;

    MySmartPoint<Point> sp3 = MySmartPoint<Point>(new Point(1,2));

    sp3 = sp;

    

    cout<< sp3->getX()<<endl;

    cout<< "over"<<endl;

    

}

0 0
原创粉丝点击