Jump to content
  • Advertisement
Sign in to follow this  
ironlungs

C++ map object and find()

This topic is 4114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a templated class that contains a member of type map and implements a Get function which uses the find() method of a map to get that object, what should be done if the object is not found? What should I return? Here's my function:
const T& Get(const std::string& key)
{
  typename std::map<std::string, T>::const_iterator it;
  it = _data.find(key);

  if (it != _data.end()) {
    return it->second;
  }

  // object wasn't found, what do i return?

}

The only thing I've come across is returning a null object. But then any class that potentially uses this class must implement a null object type. Is that really practical? I'm not sure if I'm comfortable with creating this kind of dependency.

Share this post


Link to post
Share on other sites
Advertisement
You could return a pointer and use a null pointer. Alternately you can throw an exception. Or you can automatically create a default constructed object in the map with the given index. Which one is the best choice is determined by how you want to use your Get() function.

Share this post


Link to post
Share on other sites
Your get function is returning "a value", not a pointer.

It's functionally identical to:

int get( std::string key );



As such, there is solution that would make sense from language perspective.

One way is to return pointers (or store pointers to value in the first place), other is to rework the logic, third is to throw an exception (natural design solution), another is to use visitor and IOC:

struct XYZ {
void operator()( const T &value ) {
std::cout << value << std::endl;
}
}

template < class Visitor >
void get( std::string &key, Visitor &visitor )
{
typename std::map<std::string, T>::const_iterator it;
it = _data.find(key);

if (it != _data.end()) {
return visitor( it->second );
}
}



And then there's a convoluted templated solution which uses in-place object wrappers to access the values, but that's an overkill.

Share this post


Link to post
Share on other sites
Before you throw an exception, though, decide whether it's actually an exceptional case (i.e. "not finding it is totally unexpected, and some weird error must have occurred [like having a typo in a config file]" as opposed to "well it's not cached yet so not found").

Another way to do it is to use a construct like boost::optional.

Share this post


Link to post
Share on other sites
Thanks for the responses everyone, I decided to return a pointer to the object or null if not found.

I think I may be able to just return a default object since I am using smart pointers as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!