Commit aa035961b38b6f01090e6f6a2ee4c9b9fb5041e8
1 parent
047bcfca
add * and -> operators
git-svn-id: svn+q:///qpdf/trunk@1029 71b93d88-0707-0410-a8cf-f5a4172ac649
Showing
3 changed files
with
40 additions
and
0 deletions
include/qpdf/PointerHolder.hh
| ... | ... | @@ -124,6 +124,24 @@ class PointerHolder |
| 124 | 124 | return this->data->refcount; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | + T const& operator*() const | |
| 128 | + { | |
| 129 | + return *this->data->pointer; | |
| 130 | + } | |
| 131 | + T& operator*() | |
| 132 | + { | |
| 133 | + return *this->data->pointer; | |
| 134 | + } | |
| 135 | + | |
| 136 | + T const* operator->() const | |
| 137 | + { | |
| 138 | + return this->data->pointer; | |
| 139 | + } | |
| 140 | + T* operator->() | |
| 141 | + { | |
| 142 | + return this->data->pointer; | |
| 143 | + } | |
| 144 | + | |
| 127 | 145 | private: |
| 128 | 146 | void init(Data* data) |
| 129 | 147 | { | ... | ... |
libtests/pointer_holder.cc
| ... | ... | @@ -12,6 +12,7 @@ class Object |
| 12 | 12 | Object(); |
| 13 | 13 | ~Object(); |
| 14 | 14 | void hello(); |
| 15 | + void hello() const; | |
| 15 | 16 | |
| 16 | 17 | private: |
| 17 | 18 | static int next_id; |
| ... | ... | @@ -38,8 +39,21 @@ Object::hello() |
| 38 | 39 | std::cout << "calling Object::hello for " << this->id << std::endl; |
| 39 | 40 | } |
| 40 | 41 | |
| 42 | +void | |
| 43 | +Object::hello() const | |
| 44 | +{ | |
| 45 | + std::cout << "calling Object::hello const for " << this->id << std::endl; | |
| 46 | +} | |
| 47 | + | |
| 41 | 48 | typedef PointerHolder<Object> ObjectHolder; |
| 42 | 49 | |
| 50 | +void callHello(ObjectHolder const& oh) | |
| 51 | +{ | |
| 52 | + oh.getPointer()->hello(); | |
| 53 | + oh->hello(); | |
| 54 | + (*oh).hello(); | |
| 55 | +} | |
| 56 | + | |
| 43 | 57 | int main(int argc, char* argv[]) |
| 44 | 58 | { |
| 45 | 59 | std::list<ObjectHolder> ol1; |
| ... | ... | @@ -74,6 +88,9 @@ int main(int argc, char* argv[]) |
| 74 | 88 | } |
| 75 | 89 | |
| 76 | 90 | ol1.front().getPointer()->hello(); |
| 91 | + ol1.front()->hello(); | |
| 92 | + (*ol1.front()).hello(); | |
| 93 | + callHello(ol1.front()); | |
| 77 | 94 | ol1.pop_front(); |
| 78 | 95 | std::cout << "goodbye" << std::endl; |
| 79 | 96 | return 0; | ... | ... |
libtests/qtest/ph/ph.out
| ... | ... | @@ -7,6 +7,11 @@ equal okay |
| 7 | 7 | less than okay |
| 8 | 8 | created Object, id 3 |
| 9 | 9 | calling Object::hello for 1 |
| 10 | +calling Object::hello for 1 | |
| 11 | +calling Object::hello for 1 | |
| 12 | +calling Object::hello const for 1 | |
| 13 | +calling Object::hello const for 1 | |
| 14 | +calling Object::hello const for 1 | |
| 10 | 15 | goodbye |
| 11 | 16 | destroyed Object, id 3 |
| 12 | 17 | destroyed Object, id 1 | ... | ... |