Re: What should function returning a reference return on failure?
Phlip wrote:
Alf P. Steinbach wrote:
* Phlip:
4) pass an optional sentinel object into FindMap, and return that.
5) return an iterator, and check if that == .end().
If the caller passes no sentinel, construct the NullObject one and
return it:
CMap& FindMap(
const unsigned int MapNumber,
CMap const & sentinel = NullMap() );
Oh, my. That's not const-correct. What's the fix?
Leave out the 'const'.
CMap & found = FindMap(2);
Now found refers to the temporary NullMap(), which destructed somewhere
around ;.
Should not work - cannot bind an rvalue (temporary) to a non-constant
reference. I know it compiles under MS VC++ 7.1, but that is erroneous.
This leaves you with this option (or an exception):
....
{
std::map<unsigned int, CMap*>::iterator ThisMapIt =
World.Maps.find( ThisPlayer.Character.Map );
if ( ThisMapIt != World.Maps.end() )
{
return *((*ThisMapIt).second);
}
else
{
//Same base type as CMap, but you know that!
static NullMap sentinal;
return sentinal;
}
}
--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!