数组模板类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/linux_Allen/article/details/80154042

数组模板类

数组类的创建主要是为了代替原生数组的使用,数组类可以通过重载数组操作符避免越界访问,提供数组长度信息,提供数组对象之间的复制操作。

  • Array.h 接口类
  • StaticArray.h 固定大小的数组类,内存在栈上分配
  • DynamicArray.h 大小可变的数组类,类似stl中的vector,内存在堆中分配

//Array.h
template <typename T>
class Array
{
  protected:
    T *m_array;

  public:
    virtual bool set(int i, const T &e)
    {
        bool ret = true;
        if (i >= 0 && i < length())
        {
            m_array[i] = e;
        }
        else
        {
            ret = false;
        }
        return ret;
    }

    virtual bool get(int i, T &e) const
    {
        bool ret = true;
        if (i >= 0 && i < length())
        {
            e = m_array[i];
        }
        else
        {
            ret = false;
        }
        return ret;
    }

    T &operator[](int i)
    {
        if (i >= 0 && i < length())
        {
            return m_array[i];
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "paramter i is invalid...");
        }
    }

    T operator[](int i) const
    {
        return const_cast<Array<T> &>(*this)[i];
    }

    virtual int length() const = 0;
};
//StaticArray.h
 template <typename T,int N>
  class StaticArray : public Array<T>
   {
   protected:
       T m_space[N];
   public:
       StaticArray ()
       {
           this->m_array = m_space;
       }

       StaticArray (const StaticArray<T,N>& obj)
       {
           this->m_array = m_space;

           for(int i = 0; i < length(); ++i)
           {
               m_space[i] = obj.m_space[i];
           }
       }

       StaticArray<T,N>& operator = (const StaticArray<T,N>& obj)
       {
           if(this != &obj)
           {
               for(int i=0; i < length(); ++i)
               {
                   m_space[i] = obj.m_space[i];
               }
           }
           return *this;
       }

       int length() const
       {
           return N;
       }
   };
//DynamicArray.h
template <typename T>
class DynamicArray :public Array<T>
{
protected:
    int m_length;

    T* copy(T* array, int len, int newlen)
    {
        T* ret = new T[newlen];
        if(ret != NULL)
        {
            int size = (len < newlen) ? len : newlen;

            for(int i=0; i<size; i++)
            {
                ret[i] = array[i];
            }
        }
        return ret;
    }

    void update(T* array, int length)
    {
        if(array != NULL)
        {
            T* temp = this->m_array;

            this->m_array = array;
            this->m_length = length;

            delete [] temp;
        }
        else
        {
                THROW_EXCEPTION(NoEnoughtMemoryException,"No memory to create DynamicArray object....");
        }
    }

    void init(T* array, int length)
    {
        if(array != NULL)
        {
            this->m_array = array;
            this->m_length = length;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughtMemoryException,"No memory to create DynamicArray object....");
        }
    }

public:
    DynamicArray(int length)
    {
        init(new T[length],length);
    }

    DynamicArray(const DynamicArray<T>& obj)
    {
        init(copy(obj.m_array,obj.m_length,obj.m_length),obj.m_length);
    }

    DynamicArray<T>& operator = (const DynamicArray<T>& obj)
    {
        if(this != &obj)
        {
            update(copy(obj.m_array,obj.m_length,obj.m_length),obj.m_length);
        }
        return *this;
    }

    int length() const
    {
        return m_length;
    }

    void resize(int length)
    {
        if(this->m_length != length)
        {
            update(copy(this->m_array,this->m_length,length), length);
        }
    }

    ~DynamicArray()
    {
        delete[] this->m_array;
    }

};

猜你喜欢

转载自blog.csdn.net/linux_Allen/article/details/80154042