From aa035961b38b6f01090e6f6a2ee4c9b9fb5041e8 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 24 Sep 2010 19:09:22 +0000 Subject: [PATCH] add * and -> operators git-svn-id: svn+q:///qpdf/trunk@1029 71b93d88-0707-0410-a8cf-f5a4172ac649 --- include/qpdf/PointerHolder.hh | 18 ++++++++++++++++++ libtests/pointer_holder.cc | 17 +++++++++++++++++ libtests/qtest/ph/ph.out | 5 +++++ 3 files changed, 40 insertions(+) diff --git a/include/qpdf/PointerHolder.hh b/include/qpdf/PointerHolder.hh index ad5d1039..6ff69117 100644 --- a/include/qpdf/PointerHolder.hh +++ b/include/qpdf/PointerHolder.hh @@ -124,6 +124,24 @@ class PointerHolder return this->data->refcount; } + T const& operator*() const + { + return *this->data->pointer; + } + T& operator*() + { + return *this->data->pointer; + } + + T const* operator->() const + { + return this->data->pointer; + } + T* operator->() + { + return this->data->pointer; + } + private: void init(Data* data) { diff --git a/libtests/pointer_holder.cc b/libtests/pointer_holder.cc index 173a8266..c255df5a 100644 --- a/libtests/pointer_holder.cc +++ b/libtests/pointer_holder.cc @@ -12,6 +12,7 @@ class Object Object(); ~Object(); void hello(); + void hello() const; private: static int next_id; @@ -38,8 +39,21 @@ Object::hello() std::cout << "calling Object::hello for " << this->id << std::endl; } +void +Object::hello() const +{ + std::cout << "calling Object::hello const for " << this->id << std::endl; +} + typedef PointerHolder ObjectHolder; +void callHello(ObjectHolder const& oh) +{ + oh.getPointer()->hello(); + oh->hello(); + (*oh).hello(); +} + int main(int argc, char* argv[]) { std::list ol1; @@ -74,6 +88,9 @@ int main(int argc, char* argv[]) } ol1.front().getPointer()->hello(); + ol1.front()->hello(); + (*ol1.front()).hello(); + callHello(ol1.front()); ol1.pop_front(); std::cout << "goodbye" << std::endl; return 0; diff --git a/libtests/qtest/ph/ph.out b/libtests/qtest/ph/ph.out index 37f63822..1326e489 100644 --- a/libtests/qtest/ph/ph.out +++ b/libtests/qtest/ph/ph.out @@ -7,6 +7,11 @@ equal okay less than okay created Object, id 3 calling Object::hello for 1 +calling Object::hello for 1 +calling Object::hello for 1 +calling Object::hello const for 1 +calling Object::hello const for 1 +calling Object::hello const for 1 goodbye destroyed Object, id 3 destroyed Object, id 1