That is absolutely the wrong thing to do. There is no such concept as a "null iterator" in the standard library. An iterator has to always point at something valid, whether it is the 1st item in a container/range, the end of a container/range, or anything in between.
For what you are attempting, you would need to wrap the iterator inside a std::optional (or equivalent, like Roger Cigol suggested), eg:
std::optional<std::list<MyType*>::const_iterator> mp1MyType;
void MyTypes::first()
{
mp1MyType = std::nullopt();
}
bool MyTypes::next()
{
if ( !mp1MyType.has_value() )
mp1MyType = mLstMyTypes.begin();
else
++(*mp1MyType);
if ( *mp1MyType == mLstMyTypes.end() )
{
mp1MyType = std::nullopt();
return false;
}
return true;
}
MyType* MyTypes::getPresent() const
{
return mp1MyType.has_value() ? *(*mp1MyType) : nullptr;
}
But, why are you waiting until next() is called to move the iterator to the 1st item? Why can't you do it in first() instead? Seems like first() should return a bool indicating whether the iterator is valid, just like next() does, eg:
std::list<MyType*>::const_iterator mp1MyType;
bool MyTypes::first()
{
mp1MyType = mLstMyTypes.begin();
return ( mp1MyType != mLstMyTypes.end() );
}
bool MyTypes::next()
{
if ( mp1MyType != mLstMyTypes.end() )
{
++mp1MyType;
if ( mp1MyType != mLstMyTypes.end() )
return true;
}
return false;
}
MyType* MyTypes::getPresent() const
{
return ( mp1MyType != mLstMyTypes.end() ) ? *mp1MyType : nullptr;
}