#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. 常用git指令记录

    Generating an SSH key Checking for existing SSH keys Generating a new SSH key and adding it to the s ...

  2. linux设置自动同步服务器时间

    最近遇到一个问题,由于两台服务器时间的问题,经常导致用户登录由于时间差问题而报错,再三百度,最后整理了一下修改linux定时同步的操作(本方法适用于有自己时间服务器,没有的只限于借鉴) 首先确认,我们 ...

  3. WebApplication 启动类一定要存于某个包下

    否则spring启动器 无法识别 包类目录

  4. c多线程不加锁demo

    // // Created by gxf on 2019/12/13. // #include <stdio.h> #include <stdlib.h> #include & ...

  5. 阿里云服务器搭建gitlab

    参考这位大神的博客 https://blog.csdn.net/zhaoyanjun6/article/details/79144175 安装邮件通知服务系统时,报了如下错误:send-mail: f ...

  6. 1113 form表单与css选择器

    目录 1.form表单 form元素 特点 参数 form元素内的控件 1.input的使用 2.select标签 3.textarea元素 4.autofocus属性 2.CSS 1.基础语法 cs ...

  7. JQuery实践--动画

    显示和隐藏没有动画的元素 使包装集里的元素隐藏 hide(speed,callback) speed:可选,速度.slow,normal,fastcallback:函数,可选,完成后调用的函数,无参数 ...

  8. leetcode解题报告(5):Longest Consecutive Sequence

    描述 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...

  9. [Luogu] 染色

    https://www.luogu.org/problemnew/show/P2486 qizha 为什么会wa #include <cstdio> #include <cmath& ...

  10. Onpaint()函数中绘图出现问题:当多次进入onpaint()发现次数达到一定程度就会出现窗口不能再重绘导致窗口内容损坏的现象

    我在一个按钮中调用sendmessage(wm_paint,0,0)达到36以上时,当最小化窗口然后再恢复就会发现窗口出现错误信息,而且窗口界面内容混乱不完整.原来以为是使用sleep()函数导致的问 ...