Jump to content
  • Advertisement
Sign in to follow this  
Wavesonics

Returning refrences

This topic is 4167 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

Quite honestly I haven't done much with references. And I'm refactoring some code of mine to use the STL::map instead of my own hash table. My old hash map returned pointers, and NULL if it wasn't found. The STL::map returns references, So my question is, what is the equivalent of returning a NULL pointer with references? From the limited reading I've done it doesn't seem there isn't one. So more specifically, does any one know what happens if I try to retrieve a key that doesn't exist in STL::map?

Share this post


Link to post
Share on other sites
Advertisement
There isn't one. std::map::operator[] creates when it doesn't find a key, so a reference to the newly default-constructed object will be returned. If that's not what you want to happen, use std::map::find.

Share this post


Link to post
Share on other sites
right, i was trying to find a way to check if the returned object was essentially that uninitialized one. I'm using something like:

Quote:

// Ensure the resource exists
if( m_resources.find( strParentId ) == m_resources.end() ) {
LOG->writeMsg( Logger::LVL_ERROR, "Resource entry of parent not found during child add for Resource: " + strParentId );
return;
}


Before any call to the [] operator at the moment.

is that pretty much how it's usually done?

Share this post


Link to post
Share on other sites
Pretty much. If you're going to use it right afterwards, though, no need to throw away the iterator and waste time finding it again with the [] operator:


ResourceMap::iterator ir = m_resources.find( strParentId );
if( ir == m_resources.end() ) {
LOG->writeMsg( Logger::LVL_ERROR, "Resource entry of parent not found during child add for Resource: " + strParentId );
return;
} else {
ir->doSomething();
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
ir->doSomething();


Nitpick: map iterators are pairs. It would probably look like ir->second.doSomething(); instead. [smile]

Share this post


Link to post
Share on other sites
Sometimes, it makes sense to expose a pointer-returning interface to the container:


Resource* ResourceMap::find(const std::string& id) {
iterator it = m_resources.find(id);
return (it == m_resources.end()) ? 0 : &(it->second);
}

Resource* r = myMap.find(strParentId);
if (!r) {
ZOMGTehErrorz();
} else {
r->doSomething();
}

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!