数组类

 数组类:实现越界检测,数组对相间的赋值,数组长度值访问

array.h

/*
 *    Array: 数组类接口
 *    成员变量:
 *                m_array    指向子类的实体数组地址
 *  成员函数:
 *                set        设置数组某一项值
 *                get           获取数组某一项值
 *                length     纯虚函数
 *                operator[]
*/

#ifndef ARRAY_H
#define ARRAY_H

#include "TopClass.h"
#include "Exception.h"


namespace DSL
{
    template <typename T>
    class Array: public TopClass
    {
        protected:
                T* m_array;
        public:
                virtual bool set(int pos, const T& obj)
                {
                    if((pos >= 0) && ( pos < length()))
                    {
                        m_array[pos] = obj;    
                        return 1;
                    }
                    else
                    {
                        return false;
                    }
                }

                virtual bool get(int pos, T& obj) const
                {
                    if((pos >= 0) && ( pos < length()))
                    {
                        obj = m_array[pos];    
                        return 1;
                    }
                    else
                    {
                        return false;
                    }
                }

                T& operator[] (int pos)
                {
                    if((pos >= 0) && (pos < length()))
                    {
                        return m_array[pos];
                    }
                    else
                    {
                        THROW_EXCEPTION(IdexOutOfBoundException, "error: index is out of bound!");
                    }
                }

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

                virtual int length() const = 0 ;
    };

}

#endif
View Code

StaticArray.h

/*
*    StaticArray: 静态数组
*    成员变量:
*                m_space  定义数组实体
*            
*    成员函数:
*                StaticArray 构造函数
*                StaticArray 拷贝构造函数
*                operator=   赋值重载函数
*                length()    长度函数
*/
#ifndef STATICARRAY_H
#define STATICARRAY_H

#include"Array.h"

namespace DSL
{
    template <typename T, int N>
    class StaticArray : public Array<T>
    {
        protected:
                T m_space[N];    // 使用C++的原生数组
        public:
                StaticArray()
                {
                    this->m_array = m_space;
                }
                
                StaticArray(const StaticArray<T,N>& obj)
                {
                    this->m_array = m_space;
                    for(int i = 0; i<N; 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 < N; i++)
                        {
                            m_space[i] = obj.m_space[i];    
                        }
                    }
                    else
                    {
                        return *this;
                    }
                }

                int length() const
                {
                    return N;
                }
    };
}
#endif
View Code

DynamicArray.h

/*
*    DynamicArray:动态数组模板
*    成员变量:
*                m_length   动态数组的实时长度
*    成员函数:
*                DynamicArray()   申请堆空间,初始化成员变量    
*                DynamicArray()   申请堆空间,初始化堆空间,初始化成员变量  
*                operator=()      申请堆空间,初始化堆空间,初始化成员变量,释放原始堆空间  
*                length()         返回动态数组长度
*                resize()         申请堆空间,初始化堆空间,初始化成员变量,释放原始堆空间
*                ~DynamicArray()  释放堆空间
*/

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include"Array.h"
#include"Exception.h"

namespace DSL
{
    template <typename T>
    class DynamicArray: public Array<T>
    {
        protected:
                int m_length;
        public:
                DynamicArray(int length)
                {
                    this->m_array = new T[length];
                    if(this->m_array != NULL)
                    {
                        this->m_length =  length;
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException,"error: no enough memory to init array");
                    }
                }

                DynamicArray(const DynamicArray<T>& obj)
                {
                    this->m_array = new T[obj.m_length];
                    if(this->m_array != NULL)
                    {
                        this->m_length = obj.m_length;
                        for(int i = 0; i < obj.m_length; i++)
                        {
                            this->m_array[i] = obj.m_array[i];
                        }
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to copy array");
                    }
                }

                DynamicArray<T>& operator=(DynamicArray<T>& obj)
                {
                    if(&obj != this)
                    {
                        T* array = new T[obj.m_length];
                        if(array != NULL)
                        {
                            for(int i = 0; i++; i<0)
                            {
                                array[i] = obj.m_array[i];
                            }
                            T* temp = this->m_array;  // 异常安全
                            this->m_array = array;
                            this->m_length = obj.m_length;
                            delete[] temp;
                        }
                        else
                        {
                            THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to assignment array");
                        }
                    }
                    return *this;
                }

                int length() const
                {
                    return m_length;
                }

                void resize(int new_length)
                {
                    if(new_length != m_length)
                    {
                        T* array = new T[m_length];
                        if(array != NULL)
                        {
                            int size =  (m_length > new_length) ? new_length : m_length;
                            for(int i = 0; i < size; i++)
                            {
                                array[i] = this->m_array[i];
                            }
                            T* temp = this->m_array;
                            this->m_array = array;
                            this->m_length = new_length;
                            delete[] temp;
                        }
                        else
                        {
                            THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory to resize array");
                        }
                        
                    }

                }

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

}

#endif
View Code

DynamicArray.h

/*
*    DynamicArray:动态数组模板
*    成员变量:
*                m_length   动态数组的实时长度
*    成员函数:
*                init()      初始化成员变量,抛出异常
*                copy()      申请堆空间,初始化堆空间
*                update()    初始化成员变量,释放堆空间
*
*                DynamicArray()   申请堆空间,初始化成员变量    
*                DynamicArray()   申请堆空间,初始化堆空间,初始化成员变量  
*                operator=()      申请堆空间,初始化堆空间,初始化成员变量,释放原始堆空间  
*                length()         返回动态数组长度
*                resize()         申请堆空间,初始化堆空间,初始化成员变量,释放原始堆空间
*                ~DynamicArray()  释放堆空间
*/

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H

#include"Array.h"
#include"Exception.h"

namespace DSL
{
    template <typename T>
    class DynamicArray: public Array<T>
    {
        protected:
                int m_length;

                void init(T* array, int length)
                {
                    if(array != NULL)
                    {
                        this->m_array = array;
                        this->m_length = length;
                    }
                    else
                    {
                        THROW_EXCEPTION(NotEnoughMemoryException, "error: no enough memory ");
                    }
                }
                
                T* copy(T* array, int length, int new_length)
                {
                    T* ret = new T[new_length];
                    if(ret != NULL)
                    {
                        int size = (new_length > length ? length : new_length);
                        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;
                    }
                }
                
        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=(DynamicArray<T>& obj)
                {
                    if(&obj != this)
                    {
                        init(copy(obj.m_array,obj.m_length,obj.m_length), obj.m_length);
                    }
                    return *this;
                }

                int length() const
                {
                    return m_length;
                }

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

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

}

#endif
View Code
原文地址:https://www.cnblogs.com/zsy12138/p/11027722.html