#include <iostream>

using namespace std;

const int DefaultSize = 10;

class Array
{
public:
  Array(int itsSize=DefaultSize);
  ~Array()
  {
    delete[] pType;
  }

  //运算符重载
  int& operator[](int offset);
  const int& operator[](int offset) const;
  
  int GetItsSize() const
  {
    return itsSize;
  }

  class ArrayIndexOutOfBoundsException {};
  class ElementZero{};

private:
  int *pType;
  int itsSize;
};

Array::Array(int size) :itsSize(size)
{
  if (size==0)
  {
    throw ElementZero();
  }
  
  pType = new int[size];
  for (int i=0;i<size;i++)
  {
    pType[i] = 0;
  }
}

int& Array::operator[](int offset)
{
  int vcsize =GetItsSize();
  if (offset>=0 && offset<vcsize)
  {
    return pType[offset];
  }else{
    throw ArrayIndexOutOfBoundsException();
  }

}

const int& Array::operator[](int offset) const
{
  int vcsize = this->GetItsSize();
  if (offset >= 0 && offset<vcsize)
  {
    return pType[offset];
  }
  else {
    throw ArrayIndexOutOfBoundsException();
  }
}

int main()
{
  Array a;
  Array b(12);
  b[2] = 10;

  cout << b[2]<< endl;

  Array arr1(20);
  try
  {
    for (int k=0;k<100;k++)
    {
      arr1[k] = k;
    }
  }
  catch (Array::ArrayIndexOutOfBoundsException)
  {
    cout<<"Array Index Out Of Bounds Exception..."<<endl;
  }

  system("pause");
  return 0;
}

-------------------------------------------------------------------------------------

10
Array Index Out Of Bounds Exception...
请按任意键继续. . .

c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)的更多相关文章

  1. C#学习之自定义数组及其排序

    在C#中对数组的定义比较灵活.这里着重说一下自定义数组和Array类的排序. 在Array类中通过属性Length就可以获取整个数组中数据的数量,可以通过foreach迭代数组. 使用Rank属性可以 ...

  2. Java中的自定义数组队列

    在Java中,作为所有数据结构中存储和获取速度最快的一种,数组凭借其这种简单易用的优势在各个方面都能大显神威.但是数组也有自身的局限性.数组的长度必须是固定的一旦定义之后就无法动态的更改,这就会造成这 ...

  3. 墨菲定律与 IndexOutOfBoundsException(数组越界异常)

    今天维护又反馈了一问题过来,查询试卷时报数组越界异常: 2017-02-28 10:45:24,827[ERROR] HttpException[10.32.111.7:60446:2D07867BE ...

  4. Java实现自定义数组及其方法

    自定义数组 主要功能有增.删(根据索引,根据值).改.查扩容等功能 package array; public class CustomArray { private int[] array = nu ...

  5. C++ 数组的地址问题学习随笔

    二维数组额地址问题学习,本文学习内容参考:http://blog.csdn.net/wwdlk/article/details/6322439 #include<iostream> usi ...

  6. yii CListView中使用CArrayDataProvider自定义数组作为数据

    CArrayDataProvider类手册: http://www.yiichina.com/api/CArrayDataProvider 在yii中无论是CListView还是CGridView,对 ...

  7. 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)

    数组下标越界异常 查看调用的数组或者字符串的下标值是不是超出了数组的范围,一般来说,显示(即直接用常数当下标)调用不太容易出这样的错,但隐式(即用变量表示下标)调用就经常出错了,还有一种情况,是程序中 ...

  8. python3.4中自定义数组类(即重写数组类)

    '''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...

  9. 学JAVA二十一天,自定义数组

    今天就说一下自定义数组,至于要怎么用,我也不知道,反正逼格挺高的. 闲话不多说,开始: 首先,自定义数组首先要创建一个类,用来做自定义数组的类型. public class User{ private ...

随机推荐

  1. linux命令返回值 / $?

    原文:http://blog.csdn.net/wyabc1986/article/details/7876673 在 Linux 下,不管你是启动一个桌面程序也好,还是在控制台下运行命令,所有的程序 ...

  2. LAMP源码编译安装

    php加速器 XCache 快速而且稳定的PHP opcode缓存,经过严格测试且被大量用于生产环境. 项目地址:http://xcache.lighttpd.net/,收录EPEL源 实现XCach ...

  3. PAT Advanced 1073 Scientific Notation (20 分)

    Scientific notation is the way that scientists easily handle very large numbers or very small number ...

  4. hive中计算某个日期是星期几的算法

    pmod(floor((unix_timestamp('2019-06-11 00:00:00')-unix_timestamp('1970-01-05 00:00:00'))/(3600*24)), ...

  5. PHP返回JSON数据及中文编码问题的解决方案

    在处理app接口的时候 ,中文在经过json_encode之后 变成\ \格式 想在返回接口的时候  中文不被转换 解决办法 第一种解决办法 exit(json_encode($result,JSON ...

  6. phpstudy修改端口及网站根目录和访问 localhost 显示目录文件夹

    一.其它选项菜单=>phpStudy设置=>端口常规设置(勾选允许目录列表): 二. Apache http端口:80 网站目录:D:\phpStudy\PHPTutorial\WWW 默 ...

  7. 27、AOP-AOP功能测试

    27.AOP-AOP功能测试 AOP : [动态代理]指程序运行期间动态的将某段代码切入到制定方法位置进行运行的编程方式. 导入AOP模块:Spring AOP(spring-aspects) 定义一 ...

  8. Vue 组件中锚点定位的问题

    1 当前组件的顶部 this.$el.scrollIntoView() 2 指定的 Element this.$el.querySelector(selector).scrollIntoView() ...

  9. 启动文件startup_stm32f40_41xxx.s

    一.启动文件,startup_stm32f40x_41xx.s 1.定义 启动文件由汇编编写,是系统上电复位后第一执行的程序. Stack_Size EQU 0x00000400 // 栈的大小可以调 ...

  10. 路由器配置——基于链路的OSPF的MD5口令认证

    一.实验目的:掌握基于链路的OSPFMD5口令认证 二.拓扑图: 三.具体步骤配置: (1)R1路由器的配置 Router>enable Router#configure terminal En ...