-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedFriend.cpp
More file actions
84 lines (70 loc) · 1.2 KB
/
Copy pathNestedFriend.cpp
File metadata and controls
84 lines (70 loc) · 1.2 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
#include <iostream>
#include <cstring>
const int sz = 50;
struct Holder {
private:
int a[sz];
public:
void initialize ( );
struct Pointer; //incomplete type spec
friend Pointer;
struct Pointer {
private:
Holder* h;
int* p;
public:
void init ( Holder* h );
void next ( );
void previous ( );
void top ( );
void end ( );
int read ( );
void set ( int i );
};
};
void Holder::initialize () {
memset(a, 0, sz * sizeof(int));
}
void Holder::Pointer::init (Holder* rv) {
h = rv;
p = rv->a;
}
void Holder::Pointer::next () {
if (p > &(h->a[0])) p++;
}
void Holder::Pointer::previous () {
if (p > &(h->a[0])) p--;
}
void Holder::Pointer::top () {
p = &(h->a[0]);
}
void Holder::Pointer::end () {
p = &(h->a[sz -1]);
}
int Holder::Pointer::read () {
return *p;
}
void Holder::Pointer::set (int i) {
*p = i;
}
int main (int argc, char** argv) {
Holder h;
Holder::Pointer hp, hp2;
int i;
h.initialize();
hp.init(&h);
hp2.init(&h);
for (i = 0; i < sz; i++) {
hp.set(i);
hp.next();
}
hp.top();
hp2.top();
for (i = 0; i < sz; i++) {
std::cout << "hp = " << hp.read()
<< ", hp = " << hp2.read()
<< std::endl;
hp.next();
hp2.previous();
}
}