完成DynamicArray类的具体实现

DynamicArray设计要点
——类模板
  动态确定内部数组空间的大小
  实现函数返回数组长度
  拷贝构造和赋值操作

DynamicArray类的声明

template <typename T>
class DynamicArray : public Array<T>
{
protected:
T m_length;
public:
DynamicArray(int length);
//拷贝构造和赋值操作
DynamicArray(const DynamicArray<T>& obj );
DynamicArray<T>& operator= (const DynamicArray<T>& obj); int length() const;
void resize(int length); //动态重置数组的长度 ~DynamicArray();
};
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H #include "Array.h"
#include "Exception.h" namespace DTLib
{
template <typename T>
class DynamicArray : public Array<T>
{
protected:
T m_length;
public:
DynamicArray(int length)
{
this->m_array = new T[length]; if(this->m_array != NULL)
{
this->m_length = length;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to create DynamicArray object...");
}
}
//拷贝构造和赋值操作
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=; i<m_length; i++)
{
this->m_array[i] = obj.m_array[i];
}
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to create DynamicArray object...");
}
}
DynamicArray<T>& operator= (const DynamicArray<T>& obj)
{
if(this != &obj)
{
T* array = new T[obj.m_length]; if(array != NULL)
{
for(int i=; i<obj.m_length; i++)
{
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(IndexOutOfBoundsException, "No memory to copy DynamicArray object...");
}
} return *this;
} int length() const
{
return m_length;
}
void resize(int length) //动态重置数组的长度
{
if(m_length != length)
{
T* array = new T[length]; if(array != NULL)
{
int size = (length < m_length) ? length : m_length; for(int i=; i<size; i++)
{
array[i] = this->m_array[i];
} T* temp = this->m_array;
this->m_array = array;
this->m_length = length; delete[] temp;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "No memory to resize DynamicArray object...");
}
}
} ~DynamicArray()
{
delete[] this->m_array;
}
}; } #endif // DYNAMICARRAY_H

测试:

#include <iostream>
#include "DynamicArray.h" using namespace std;
using namespace DTLib; int main()
{ DynamicArray<int> sl(); for(int i=; i<sl.length(); i++)
{
sl[i] = i * i;
} for(int i=; i<sl.length(); i++)
{
cout << sl[i] << endl;
} cout << endl;
DynamicArray <int> s2();
s2 = sl;
for(int i=; i<s2.length(); i++)
{
cout << s2[i] << endl;
} cout << endl; s2.resize();
for(int i=; i<; i++)
{
cout << s2[i] << endl;
}
return ; }

代码优化:

DynamicArray类中的函数实现存在重复的逻辑,如何进行代码优化?

重复代码逻辑的抽象
——init
  对象构造时的初始化操作
——copy
  在堆空间中申请新的内存,并执行拷贝操作
——update
  将指定的堆空间作为内部存储数组使用

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H #include "Array.h"
#include "Exception.h" namespace DTLib
{
template <typename T>
class DynamicArray : public Array<T>
{
protected:
T 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=; 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(IndexOutOfBoundsException, "No memory to resize DynamicArray object...");
}
} void init(T* array, int length)
{
if(array != NULL)
{
this->m_array = array;
this->m_length = length;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "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(m_length != length)
{
update(copy(this->m_array, m_length ,length),length);
}
} ~DynamicArray()
{
delete[] this->m_array;
}
}; } #endif // DYNAMICARRAY_H

最新文章

  1. RPC通信框架&mdash;&mdash;RCF介绍
  2. [ZigBee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁分析(下)
  3. 用File判断D盘下面是否还有txt文件
  4. TCP协议与UDP协议的区别
  5. winform开发之UI系列
  6. RDIFramework.NET ━ 9.14 数据库连接管理 ━ Web部分
  7. APP 上架苹果应用商城
  8. Web Service 接口实现大量数据传输的解决方案
  9. 【JSP】JSP向MySQL写入|读出中文数据——乱码问题
  10. 服务端生成word并压缩打包下载
  11. sublime sftp 插件安装及破解
  12. Linux下多线程查看工具(pstree、ps、pstack) (转)
  13. Android自定义Seekbar滑动条,Pop提示跟随滑动按钮一起滑动
  14. ecshop 修改记录20150710
  15. windows命令提示符
  16. Day07 - Ruby比一比:Symbol符号与String字串
  17. LeetCode(192. Word Frequency)
  18. 剑指offer——python【第28题】数组 中出现次数超过一半的数字
  19. idea插件JRebel 解决热编译,开启高级debug之路
  20. Struts2(五)Action二配置

热门文章

  1. 如何用css画一个文件上传图案?
  2. 【JS】328- 8个你不知道的DOM功能
  3. elasticsearch搜索QueryStringQueryBuilder时的一些问题记录
  4. JMeter之SteppingShape
  5. Vue AES+MD5加密 后台解密
  6. Java基础篇
  7. luogu1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
  8. [ASP.NET Core 3框架揭秘] 文件系统[3]:物理文件系统
  9. 4. abp中的asp.net core模块剖析
  10. Python 使用中出现错误:ImportError: No module named _sqlite3