#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. PAT Advanced 1140 Look-and-say Sequence (20 分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  2. 手把手教你在CentOS上搭建Kubernetes集群

    作者:ChamPly 安装CentOS 1.安装net-tools [root@localhost ~]# yum install -y net-tools 2.关闭firewalld [root@l ...

  3. 大数据之路week05--day02(Maven安装,环境变量的配置及基本使用)

    今天我们就来学习一下maven,怎么说呢,maven更像是一种管理的工具,实现的原理是使用插件. 举个例子,比如说,一个公司需要做一个项目,这个项目又分成了很多的模块,每个模块又分成了许多的业务等等, ...

  4. python_tkinter弹出对话框1

    tkinter提供了三个模块,可以创建弹出对话窗口:(使用必须单独导入模块) 1.messagebox 消息对话框 示例:askokcancel import tkinter # 导入消息对话框子模块 ...

  5. Python+request+ smtplib 测试结果html报告邮件发送(下)《六》

    目录结构如下: 1.cfg.ini的配置信息写法如下: [email] ;--------------------------使用腾讯企业邮箱作为发件人的操作如下------------------- ...

  6. 前端知识体系:JavaScript基础-原型和原型链-new一个对象的详细过程,手动实现一个 new操作符

    可以描述 new一个对象的详细过程,手动实现一个 new操作符 1. new 一个对象的详细过程:(原文地址) 首先我们看下new Person输出什么? var Person = function( ...

  7. 【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

    题目如下: Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square ...

  8. java获取windows和linux下本机ip通用方法

    public InetAddress getFirstNonLoopbackAddress(boolean preferIpv4, boolean preferIPv6) throws SocketE ...

  9. ueditor批量上传word图片

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-editor的二进制流保存为例:打开umeditor.js,找到UM ...

  10. bzoj 5072

    对于某一大小的连通子图包含的黑点的数目的最大值和最小值都能取到考虑树形dp$f[i][j]$ 表示从 $i$ 的子树中选出大小为 $j$ 的联通子图黑点数目的最小值$g[i][j]$ 表示从 $i$ ...