堆内存指针的管理类(禁,引数(指针copy),值copy,移)

 

 

//copyp
template<typename T>
class pm_copyP{
public:
    pm_copyP(T* _p);
    pm_copyP(const pm_copyP&);
    pm_copyP& operator=(const pm_copyP&);
    ~pm_copyP();
    T* getP()const;
    static int mmout;
private:
    pm_copyP();
    T* myp;
    int * count_ref;
    del();
};

template<typename T>
pm_copyP<T>::pm_copyP(T* _p):myp(_p),count_ref(new int(1)){++mmout;}

template<typename T>
pm_copyP<T>::pm_copyP(const pm_copyP& _pm)
{
    myp=_pm.myp;
    count_ref=_pm.count_ref;
    int* temp=count_ref;
    ++(*temp);
}

template<typename T>
pm_copyP<T>& pm_copyP<T>::operator=(const pm_copyP& _pm)
{
    if(&_pm!=this)
    {
     ++(*_pm.count_ref);//这里很危险,刚发现。就算不是自我赋值,有可能,这个类是一个包含自身职能指针的类。删除本身,又会调用里面的析构,刚好析构是参数呢?所以必须放到第一行。
        del();
        myp=_pm.myp;
        count_ref=_pm.count_ref;
    }
    return *this;
}

template<typename T>
pm_copyP<T>::~pm_copyP()
{
    del();
}

template<typename T>
pm_copyP<T>::del()
{
    --(*count_ref);
    if(*count_ref==0)
    {
        --mmout;
        delete myp;
        delete count_ref;
        myp=0;
        count_ref=0;
    }
}

template<typename T>
T* pm_copyP<T>::getP()const
{
    return myp;
}


template<typename T>
class pm_forbidP{
public:
    pm_forbidP(T* _p);
    ~pm_forbidP();
    T* getP();
private:
    T* myp;
    pm_forbidP();
    pm_forbidP(const pm_forbidP&);
    pm_forbidP& operator=(const pm_forbidP&);
};


template<typename T>
pm_forbidP<T>::pm_forbidP(T* _p):myp(_p){}


template<typename T>
T* pm_forbidP<T>::getP()
{
    return myp;
}

template<typename T>
pm_forbidP<T>::~pm_forbidP()
{
    delete myp;
}


template<typename T>
class pm_copyValue{
public:
    pm_copyValue(T* _p);
    pm_copyValue(const pm_copyValue&);
    pm_copyValue& operator=(const pm_copyValue&);
    ~pm_copyValue();
    T* getP();
private:
    pm_copyValue();
    T* myp;
    void del();
};



template<typename T>
pm_copyValue<T>::pm_copyValue(T* _p):myp(_p){}

template<typename T>
pm_copyValue<T>::pm_copyValue(const pm_copyValue& _pm):myp(new T(*_pm.myp)){}


template<typename T>
pm_copyValue<T>& pm_copyValue<T>::operator=(const pm_copyValue& _pm)
{
    if(&_pm!=this)
    {
        del();
        myp=new T(*_pm.myp);
    }
    return *this;
}

template<typename T>
pm_copyValue<T>::~pm_copyValue()
{
    del();
}

template<typename T>
void pm_copyValue<T>::del()
{
    delete myp;
}

template<typename T>
T* pm_copyValue<T>::getP()
{
    return myp;
}



template<typename T>
class pm_transforP{
public:
    pm_transforP(T* _p);
    pm_transforP(pm_transforP&);
    pm_transforP& operator=(pm_transforP&);
    ~pm_transforP();
    T* getP();
private:
    T* myp;
    void del();
    pm_transforP();
};

template<typename T>
pm_transforP<T>::pm_transforP(T* _p):myp(_p){}

template<typename T>
pm_transforP<T>::pm_transforP(pm_transforP& _pm):myp(_pm.myp)
{
    _pm.myp=0;//const 怎么可以修改.
}


template<typename T>
pm_transforP<T>& pm_transforP<T>::operator=(pm_transforP& _pm)
{
    if(this!=&_pm)
    {
        del();
        myp=_pm.myp;
        _pm.myp=0;//const 怎么可以修改.
    }
    return *this;
}

template<typename T>
pm_transforP<T>::~pm_transforP()
{
    del();
}

template<typename T>
void pm_transforP<T>::del()
{
    if(myp!=0)
    {
        delete myp;
    }
}

template<typename T>
T* pm_transforP<T>::getP()
{
    return myp;
}

 

posted @ 2016-11-13 11:17  琴鸟  阅读(265)  评论(0编辑  收藏  举报