How to overload operator-> to a "generating" iterator?

I determine the type of iterator that does not explicitly store its current value. Instead, it wraps around another iterator and returns the std::pairbase values ​​of the iterator and another value. The relevant part of the class definition:

class MyIterator : public std::iterator<input_iterator, std::pair<Foo *, Bar *> > {

    FooPtrIterator d_iter;
    Bar d_bar;

    public:

        MyIterator(FooPtrIterator iter, Bar const &bar)
        :
            d_iter(iter),
            d_bar(bar)
        { }

        // Returning by value, not storing the pair itself.
        value_type operator*() const {
            return std::make_pair(*d_iter, &d_bar);
        }

        // Can't return by value!
        ??? operator->() const {
            return ???;
        }

};

Now I am having problems with overloading operator->because I have to return a pointer or something else that itself supports it operator->.

I could just save the current std::pairas a member of the class:

    value_type d_current;

However, this causes me problems with the constructor, because I cannot know at this moment whether FooPtrIterator iterit is a valid iterator, so I cannot dereference it to provide a d_currentvalue.

operator-> return a boost::shared_ptr<value_type>, , .

, "" . , ?

+3
1

, Foo * Bar *, :

class MyPair  {
private:
  FooPtrIterator foo;
  Bar bar;

public:
  MyPair(const FooPtrIterator& it, const Bar& b) : foo(it), bar(b)  {}

  Foo* first() { return *foo; }
  Bar* second() { return &bar; }
};

. → first → second to → first() → second() , .

+2

Source: https://habr.com/ru/post/1756730/


All Articles