I have an iterator which iterates over a std::shared_ptr. So operator++ points the internally stored shared pointer to the next item.
template<class IT>
class MyIterator
{
public:
...
MyIterator& operator++()
{
_i = ... // Call factory
return *this;
}
private:
std::shared_ptr<IT> _i;
};
How should I implement the operator*() and operator->() operators?
How should I test if the iterator is pointing to NULL, i.e. if the internal shared pointer is pointing to NULL.
You should define under what circumstances users are permitted to deference an instance of your class. Usually this is “anything other than an end iterator or an uninitialized iterator”.
Then you should ensure that
_inever contains a null pointer for an instance that can be dereferenced.This means there is no need for a check, because the user is not permitted to call
operator*oroperator->in that circumstance. You could add one for debugging, for example:assert(_i.get());.You don’t specify what the
value_typeis of your iterator, but assuming that it isIT, you can implement: