-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo42.cpp
68 lines (58 loc) · 1.05 KB
/
demo42.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
const int DefaultSize = 10;
class Array
{
public:
Array(int itsSize = DefaultSize);
~Array() { delete [] pType; }
int& operator [](int idx);
const int& operator [] (int idx) const;
int size() { return itsSize; }
class xBoundary{};
private:
int *pType;
int itsSize;
};
Array::Array(int size):itsSize(size)
{
pType = new int[size];
for(int i=0; i<size; ++i)
pType[i] = 0;
}
int& Array::operator [] (int idx)
{
int size = this->size();
if(idx >= 0 && idx < size)
return pType[idx];
else
throw xBoundary();
}
const int& Array::operator [] (int idx) const
{
// int size = this->size();
int size = this->itsSize;
if(idx >= 0 && idx < size)
return pType[idx];
else
throw xBoundary();
}
int main()
{
Array a;
Array b(12);
try
{
cout << b.size() << '\t' << b[0] << endl;
b[20] = 6;
cout << "b[6] -> " << b[6] << endl;
cout << "测试一下" << endl;
}
catch(Array::xBoundary)
{
cout << "index out of range!" << endl;
}
catch(...)
{ cout << "Some Error unknown happens."; }
return 0;
}