1 DynamicArray设计要点
类模板
- 动态确定内部数组空间的大小
- 实现函数返回数组长度
- 构造拷贝和赋值操作
2 继承关系图和接口实现
继承关系图

接口实现

template < typename T >
class DynamicArray : public Array<T>
{
protected:
int m_length;
public:
DynamicArray(int length);
DynamicArray(const DynamicArray<T>& obj);
DynamicArray<T>& operator= (const DynamicArray<T>& obj);
void resize(int length);
int length() const;
~DynamicArray();
};
3 代码实现
DynamicArray.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include "Array.h"
#include "Exception.h"
namespace LemonLib {
template < typename T >
class DynamicArray : public Array<T>
{
protected:
int m_length;
public:
DynamicArray(int length = 0)
{
this->m_array = new T[length];
if (this->m_array)
{
m_length = length;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to create DynamicArray obj...");
}
}
DynamicArray(const DynamicArray<T>& obj)
{
this->m_array = new T[obj.m_length];
if (this->m_array)
{
for (int i=0; i<obj.m_length; i++)
{
this->m_array[i] = obj.m_array[i];
}
m_length = obj.m_length;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to copy DynamicArray obj...");
}
}
DynamicArray<T>& operator= (const DynamicArray<T>& obj)
{
if (this != &obj)
{
T* array = new T[obj.m_length];
if (array)
{
for (int i=0; i<obj.m_length; i++)
{
array[i] = obj.m_array[i];
}
T* tmp = this->m_array;
this->m_array = array;
m_length = obj.m_length;
delete[] tmp;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to copy DynamicArray obj...");
}
}
return *this;
}
void resize(int length)
{
if (length != m_length)
{
T* array = new T[length];
if (array)
{
int len = (m_length < length) ? m_length : length;
for (int i=0; i<len; i++)
{
array[i] = this->m_array[i];
}
T* tmp = this->m_array;
this->m_array = array;
m_length = length;
delete[] tmp;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to resize DynamicArray obj...");
}
}
}
int length() const
{
return m_length;
}
~DynamicArray()
{
delete[] this->m_array;
}
};
}
#endif // DYNAMICARRAY_H
main.cpp
#include <iostream>
#include "Object.h"
#include "Exception.h"
#include "List.h"
#include "Seqlist.h"
#include "Staticlist.h"
#include "Dynamiclist.h"
#include "Staticarray.h"
#include "DynamicArray.h"
using namespace std;
using namespace LemonLib;
int main()
{
DynamicArray<int> da(5);
for (int i=0; i<da.length(); i++)
{
da[i] = i * i;
}
for (int i=0; i<da.length(); i++)
{
cout << da[i] << endl;
}
DynamicArray<int> da1 = da;
for (int i=0; i<da1.length(); i++)
{
cout << da[i] << endl;
}
DynamicArray<int> da2(10);
da2 = da1;
for (int i=0; i<da2.length(); i++)
{
cout << da2[i] << endl;
}
da2.resize(10);
for (int i=0; i<da2.length(); i++)
{
cout << da2[i] << endl;
}
da2.resize(3);
for (int i=0; i<da2.length(); i++)
{
cout << da2[i] << endl;
}
return 0;
}
4 代码优化
可以看出DynamicArray类中函数实现存在重复的逻辑,如何进行代码优化呢?
重复代码逻辑的抽象
init:对象构造时的初始化操作。copy:在堆空间中申请新的内存,并执行拷贝操作。update:将指定的堆空间作为内部存储数组使用。
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include "Array.h"
#include "Exception.h"
namespace LemonLib {
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 len)
{
if (array != NULL)
{
T* tmp = this->m_array;
this->m_array = array;
m_length = len;
delete[] tmp;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to update DynamicArray obj...");
}
}
void init(T* array, int len)
{
if (array != NULL)
{
this->m_array = array;
m_length = len;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to init DynamicArray obj...");
}
}
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;
}
void resize(int length)
{
if (length != m_length)
{
update(copy(this->m_array, m_length, length), length);
}
}
int length() const
{
return m_length;
}
~DynamicArray()
{
delete[] this->m_array;
}
};
}
#endif // DYNAMICARRAY_H
3164

被折叠的 条评论
为什么被折叠?



