CSDN博客

img ajiva

在BREW里实现智能指针Smart Port[初级]

发表于2004/12/29 14:48:00  972人阅读

template<class T>
class CTJBrewPtr
{
public:
    typedef T element_type;
    //ctor
    explicit CTJBrewPtr(T *pVal = 0) throw()
    {
        if(pVal)
            m_AutoPtr = pVal;
        else
            m_AutoPtr = NULL;
  i = 0;
    }
    //copy ctor
    CTJBrewPtr(const CTJBrewPtr<T>& ptrCopy) throw()
    {
        if(ptrCopy)
            m_AutoPtr = ptrCopy;
        else
            m_AutoPtr = NULL;
  i = 0;
    }
    //overloading = operator
    CTJBrewPtr<T>& operator=(CTJBrewPtr<T>& ptrCopy) throw()
    {
        if(ptrCopy)
            m_AutoPtr = &ptrCopy;
        else
            m_AutoPtr = 0;
        return m_AutoPtr;
    }
    //dtor
    ~CTJBrewPtr()
    {
        if(m_AutoPtr)
        {
            delete m_AutoPtr;
        }
    }
    //overloading * operator
    T& operator*() const throw()
    {
        return *m_AutoPtr;
    }
    //overloading -> operator
    T *operator->() const throw()
    {
        return m_AutoPtr;
    }
    //function to get the pointer to the class
    T *get() const throw()
    {
        return m_AutoPtr;
    }
private:
    T *m_AutoPtr;
 uint32  i;
};
0 0

相关博文

我的热门文章

img
取 消
img