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

类别:软件工程 点击:0 评论:0 推荐:
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;
};

本文地址:http://com.8s8s.com/it/it33136.htm