How to make an iterator "automatically dereferenced"?

Suppose I have something like this:

class Collection { private: typedef std::vector< std::shared_ptr<Something> >::iterator Iterator; std::vector< std::shared_ptr<Something> > data_; public: Iterator begin() {return data_.begin()} Iterator end() {return data_.end()} } 

When I use an instance of Collection::Iterator , I need to dereference it once to get the std::shared_ptr<Something> object and get the Something object again.

But if I want to make std::shared_ptr<Something> only an implementation detail, it is reasonable that after one dereferencing I should get a Something object.

I.e:

 Collection collection; Collection::Iterator it = collection.begin(); Something firstMember = *it; // instead of the current **it; 

My question is: do I need to make Iterator as a nested Collection class from scratch and implement all the functions necessary for a random access iterator here http://www.cplusplus.com/reference/std/iterator/ or is there some known approach? Maybe C ++ 11?

+4
source share
1 answer

It looks like you are implementing and is called boost::ptr_vector . Boost also provides a library for implementing iterators with less pain. It looks like you are looking for boost::indirect_iterator

+10
source

All Articles