c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)
#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 (学习)的更多相关文章
- C#学习之自定义数组及其排序
在C#中对数组的定义比较灵活.这里着重说一下自定义数组和Array类的排序. 在Array类中通过属性Length就可以获取整个数组中数据的数量,可以通过foreach迭代数组. 使用Rank属性可以 ...
- Java中的自定义数组队列
在Java中,作为所有数据结构中存储和获取速度最快的一种,数组凭借其这种简单易用的优势在各个方面都能大显神威.但是数组也有自身的局限性.数组的长度必须是固定的一旦定义之后就无法动态的更改,这就会造成这 ...
- 墨菲定律与 IndexOutOfBoundsException(数组越界异常)
今天维护又反馈了一问题过来,查询试卷时报数组越界异常: 2017-02-28 10:45:24,827[ERROR] HttpException[10.32.111.7:60446:2D07867BE ...
- Java实现自定义数组及其方法
自定义数组 主要功能有增.删(根据索引,根据值).改.查扩容等功能 package array; public class CustomArray { private int[] array = nu ...
- C++ 数组的地址问题学习随笔
二维数组额地址问题学习,本文学习内容参考:http://blog.csdn.net/wwdlk/article/details/6322439 #include<iostream> usi ...
- yii CListView中使用CArrayDataProvider自定义数组作为数据
CArrayDataProvider类手册: http://www.yiichina.com/api/CArrayDataProvider 在yii中无论是CListView还是CGridView,对 ...
- 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)
数组下标越界异常 查看调用的数组或者字符串的下标值是不是超出了数组的范围,一般来说,显示(即直接用常数当下标)调用不太容易出这样的错,但隐式(即用变量表示下标)调用就经常出错了,还有一种情况,是程序中 ...
- python3.4中自定义数组类(即重写数组类)
'''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...
- 学JAVA二十一天,自定义数组
今天就说一下自定义数组,至于要怎么用,我也不知道,反正逼格挺高的. 闲话不多说,开始: 首先,自定义数组首先要创建一个类,用来做自定义数组的类型. public class User{ private ...
随机推荐
- MySQL在使用group_concat()函数数据被截取
遇到问题: 项目中有个需求,MySQL中存储的是树状的数据.现在给出一个节点,需要从Mysql数据库中取出这个节点下所有的节点.采用MySQL的函数. 函数如下: CREATE DEFINER=`ro ...
- 在 java 程序中怎么保证多线程的运行安全?(未完成)
在 java 程序中怎么保证多线程的运行安全?(未完成)
- sklearn逻辑回归实战
目录 题目要求 ex2data1.txt处理 方案一:无多项式特征 方案二:引入多项式特征 ex2data2.txt处理 两份数据 ex2data1.txt ex2data2.txt 题目要求 根据学 ...
- python画图matplolib
http://python.jobbole.com/85106/ 1.画二维图 2.画三维图 我的电脑只能在jupyter notebook上面运行才能看的到,常规import库 %matplotli ...
- Selenium(六)对话框处理与登录测试
1.以百度为例,找到登录框,查看元素,可以看到这是一个div 操作登录框: 通过判断是否找到这个错误提示元素查看是否登录成功. 一个简单的登录脚本:
- BZOJ 3589 动态树 (树链剖分+线段树)
前言 众所周知,90%90\%90%的题目与解法毫无关系. 题意 有一棵有根树,两种操作.一种是子树内每一个点的权值加上一个同一个数,另一种是查询多条路径的并的点权之和. 分析 很容易看出是树链剖分+ ...
- TFS命令行
tfs命令工具: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/9s5ae285 ...
- Girls and Boys POJ - 1466 【(二分图最大独立集)】
Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...
- Share Point安裝
Win7上裝不了WSS(Sharepoint3.3),而安裝SharePoint Foundation 2010時候又由於不能跑PrerequisiteInstaller.exe,很多該裝的組件裝不上 ...
- bzoj3694
/* * 对于不在最短路树上的边(x, y) * 1 * | * | * t * / \ * / \ * x-----y * 考虑这样一种形态的图, ‘-’ 标记为非最短路树的边 * 对于边集(x, ...