forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_2_mylist-iter.h
More file actions
43 lines (34 loc) · 893 Bytes
/
3_2_mylist-iter.h
File metadata and controls
43 lines (34 loc) · 893 Bytes
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
// file: 3mylist-iter.h
// #include "3_2_mylist.h"
template <class Item>
struct ListIter {
Item *ptr; // 保持与容器的联系
// default ctor
ListIter(Item* p =0) : ptr(p) {}
// 解引用 dereference
Item& operator*() const {
return *ptr;
}
// member access
Item* operator->() const {
return ptr;
}
// prefix increment, 返回对象
ListIter& operator++() {
ptr = ptr->next();
return *this;
}
// postfix increment, 返回值(新对象)
// int为占位符,提示编译器这是后自增
ListIter operator++(int) {
ListIter temp = *this;
++*this; // 调用前面的前自增
return temp;
}
bool operator==(const ListIter& i) const {
return ptr == i.ptr;
}
bool operator!=(const ListIter& i) const {
return ptr != i.ptr;
}
};