Jump to content
  • Advertisement
Sign in to follow this  
BirdGB

Return value

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

Hi,

   I am trying to convert a function. Working with pointers, my function is looks like this -

The container-

std::vector<COne*> m_OneContainer;

When I have to find something, I do like this -

COne* CScene::getOneFromId(int id)
{
   for(auto &it: m_OneContainer)
   {
      if((*it)->getID() == id)
      {
         return *it;
      }
   return 0;
}

But when I work with reference -

std::vector<COne> m_OneContainer;

Function -

COne& CScene::getOneFromId(int id)
{
   for(auto &it: m_OneContainer)
   {
      if(it.getID() == id)
      {
         return it;
      }
   return 0; // What do I return here??
}

My question is what do I return as NULL?

 

Thank you

Share this post


Link to post
Share on other sites
Advertisement

Instead of returning the COne instance, you could make a paramter for this and return true if something has been found and false when it does not exist.

bool CScene::getOneFromId(int id, COne &ret)

Share this post


Link to post
Share on other sites

You need to ask yourself what you expect to happen in your program. Is it expected behavior that sometimes you will not find a "one"? (In which case, return a null pointer, or do something like NLScotty suggests) Or is this like a failure condition indicating a bug in your program? (In which case, assert, or throw an exception).

 

Anyway, why do you want to convert it from pointers to objects?

Share this post


Link to post
Share on other sites

I want it to return NULL when it can't find anything. I am trying to avoid using "new" wherever possible. I will stick with my first method for now.

 

Thank you for the reply.

Share this post


Link to post
Share on other sites

Pointers are references that can be null. References themselves can't be null*. That's one of the strengths and guarantees* of references.

 

*Normally. You can cram an address of 0x00 into a reference's memory, but you shouldn't. It defeats the point of using a reference over a pointer.

 

Some people use a wrapper class called optional<> for that. Boost has an implementation, and C++ will likely get a standard library class like that, but that's still a "maybe" and way off in 2017.

 

If your object type has some concept of being null, empty, or invalid, you can return a reference to an invalid dummy member variable for invalid returns, but I only recommend you do that if you're returning const references or returning by value.

 

For example, if the function returns strings or std vectors, you can return an empty string or vector on failure, when it makes sense. But even this isn't the best idea, because you still don't know if the function returned an empty string because of failure, or an empty string because of success.

 

What I do in this situation, is I pass in the "fallback result" as the final parameter:

const std::string &GetSomething(const std::string &key, const std::string &fallback)
{
     if(...doesn't exist...) return fallback;
 
     return stuff[key];
}

This is for cases where failing to find a result is not considered an error. If it is actually an error, you want the caller to somehow be able to be informed about that, so you should at least be able to pass in an optional boolean pointer to set to true on error.

Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

 

*Normally. You can cram an address of 0x00 into a reference's memory, but you shouldn't. It defeats the point of using a reference over a pointer.

 

If you accept undefined behavior, then anything is possible. You cannot have a well defined null-reference.

Share this post


Link to post
Share on other sites

Can I do like this -

COne* CScene::getOneFromId(int id)
{
   for(auto &it: m_OneContainer)
   {
      if(it.getID() == id)
      {
         COne* one = &it;
         return one;
      }
   }
   return 0;
}

Thank you.

Share this post


Link to post
Share on other sites

You have to dereference the iterator to get a reference to the object, and then take the address of that reference to get a pointer to the object.

return &*it;

Just taking the address of the iterator will give you a pointer to an iterator to a COne object, but that is not what the function is suppose to return.

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!