So I'm having a rather tumultuous conversion to C++ from Java/C#. Even though I feel like I understand most of the basics, there are some big fat gaping holes in my understanding.
For instance, consider the following function:
Fruit&
FruitBasket::getFruitByName(std::string fruitName)
{
std::map<std::string,Fruit>::iterator it = _fruitInTheBascit.find(fruitName);
if(it != _fruitInTheBascit.end())
{
return (*it).second;
}
else
{
//I would so love to just return null here
}
}
Where _fruitsInTheBascit
is a std::map<std::string,Fruit>
. If I query getFruitByName("kumquat")
you know it's not going to be there - who eats kumquats? But I don't want my program to crash. What should be done in these cases?
P.S. tell me of any other stupidity that I haven't already identified.