forked from halterman/CppBook-SourceCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallchainexception.cpp
More file actions
63 lines (55 loc) · 1.71 KB
/
callchainexception.cpp
File metadata and controls
63 lines (55 loc) · 1.71 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
#include <iostream>
#include <vector>
#include <string>
// Used to keep track of object creation and destruction
class Tracker {
std::string name;
public:
Tracker(const std::string& s): name(s) {
std::cout << "Creating Tracker (" << name << ")\n";
}
~Tracker() {
std::cout << "Destroying Tracker (" << name << ")\n";
}
};
bool find(const std::vector<int>& v, int lower, int upper) {
std::cout << "------[Entering find]------\n";
Tracker obj("find"); // Local tracking object
bool result = false; // Not there by default
int seek; // What to look for
std::cout << "Enter item to locate: ";
std::cin >> seek;
for (int i = lower; i < upper; i++)
if (v.at(i) == seek) {
result = true; // Found it
break;
}
std::cout << "------[Leaving find]------\n";
return result;
}
void process(const std::vector<int>& v) {
std::cout << "------[Entering process]------\n";
Tracker obj("process"); // Local tracking object
int low, high;
std::cout << "Enter a range: ";
std::cin >> low >> high;
if (find(v, low, high))
std::cout << "Found it\n";
else
std::cout << "Not there\n";
std::cout << "------[Leaving process]------\n";
}
// Global tracking object
Tracker obj("global");
int main() {
std::cout << "------[Entering main]------\n";
Tracker obj("main"); // Local tracking object
std::vector<int> nums { 11, 42, 23 };
try {
process(nums);
}
catch (std::exception& e) {
std::cout << "nums vector bounds exceeded\n";
}
std::cout << "------[Leaving main]------\n";
}