forked from ashishlal/c--
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_2.cpp
More file actions
98 lines (92 loc) · 2.39 KB
/
14_2.cpp
File metadata and controls
98 lines (92 loc) · 2.39 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <exception>
#include <stdexcept>
template <typename T>
class Ptr_to_T {
T *p;
T *array;
int size;
public:
// bind to array v of size s, initial val p
Ptr_to_T(T *p1, T* v, int s): p(p1), array(v), size(s) {};
Ptr_to_T(T *p1) {};
Ptr_to_T & operator++ () throw() {
if(((char *)p - (char *)array) >= size) {
throw std::out_of_range("++ takes pointer out of range");
return *this;
}
p++;
return *this;
}
Ptr_to_T & operator++ (int) throw() { // postfix
if(((char *)p - (char *)array) >= size) {
throw std::out_of_range("++ takes pointer out of range");
return *this;
}
p++;
return *this;
}
Ptr_to_T & operator-- () throw() { // prefix
if(((char *)p - (char *)array) <= 0) {
try {
throw std::out_of_range("-- takes pointer out of range");
return *this;
}
catch (const std::exception &e) {
std::cout << "caught exception.." << e.what() << "\n";
}
return *this;
}
p--;
return *this;
}
Ptr_to_T & operator-- (int) throw() { // postfix
if(((char *)p - (char *)array) <= 0) {
try {
throw std::out_of_range("-- takes pointer out of range");
return *this;
}
catch (const std::exception &e) {
std::cout << "caught exception.." << e.what() << "\n";
}
}
p--;
return *this;
}
T& operator* () { return *p; } ; // prefix
};
void term()
{
std::cout << "Inside term.." << std::endl;
try {
;
}
catch (const std::exception &e) {
std::cout << "caught exception.." << e.what() << "\n";
}
catch (...) {
std::cout << "all handled here!"<< std::endl;
}
}
int main()
{
int i[200];
for(int k = 0; k < 200; k++) {
i[k] = k;
}
std::set_terminate(term);
try {
Ptr_to_T<int> j(&i[0], i, 200);
j++;
std::cout << *j << std::endl;
j--;
std::cout << *j << std::endl;
j--;
}
catch (const std::exception &e) {
std::cout << "caught exception.." << e.what() << "\n";
}
catch (...) {
std::cout << "all handled here!"<< std::endl;
}
}