如有错误,欢迎指正,谢谢

template<class T>
class smart_ptr{
public:
    smart_ptr(T* ptr=0):m_ptr(ptr){
        if(m_ptr)
            m_count=new unsigned (1);
    }
    smart_ptr(const smart_ptr& src){
        if(this!=&src){
            m_ptr=src.m_ptr;
            m_count=src.m_count;
            (*m_count)++;
        }
    }
    ~smart_ptr(){
        release();
    }
    smart_ptr operator = (const smart_ptr& src){
        if(this!=&src){
            release();
            m_ptr=src.m_ptr;
            m_count=src.m_count;
            (*m_count)++;
        }
        return *this;
    }
    T& operator [] (unsigned index){
        return m_ptr[index];
    }
    T& operator * (){
        if(m_ptr)
            return *m_ptr;
        throw "NullPointerError";
    }
    T* operator -> (){
        if(m_ptr)
            return m_ptr;
        throw "NullPointerError";
    }

private:
    T* m_ptr;
    unsigned* m_count;
    void release(){
        if(m_ptr){
            (*m_count)--;
            if(m_count==0){
                delete []m_ptr;
                delete m_count;
            }
        }
        else if(m_count)
            delete m_count;
    }
};