Is there a way to implement operator->, not only operator*. To have following code working:
Iterator<value> it = ...
i = (*it).get();
i = it->get(); // also works
Let we say that value type has method get. When Iterator is implemnted as below:
template<T> class Iterator {
T operator*() { return ... }
T operator->() { return ... }
}
Here ... is an implementation of getting right T object.
Somehow it won't work when I implement it in this way. I think I misunderstand something.
operator->
should return a pointer:
T * operator->();
T const * operator->() const;
operator*
should return a reference if you want to use it for modification:
T & operator*();
T operator*() const; // OR T const & operator*() const;
As odd as this may seem, you want to return a pointer to T, thusly:
T * operator->() { return &the_value; }
Or a pointer to const.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With