Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

robinei

NULL reference..

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

When a function is supposed to return a reference to an object and something goes wrong, is there something one can do that is equivalent to returning NULL when dealing with pointers?

Share this post


Link to post
Share on other sites
Advertisement
NULL references are evil . Either return it as a pointer since so that the caller must check for its validity or throw an exception (there are a couple "std" exception types that might be what you want to use, or a custom "library" exception of some variety) unless you have a non-return value way of flagging an error.

Share this post


Link to post
Share on other sites
To do that I guess at least som heavy casting would be required.

Just returning NULL I get this:

mapview.cpp:35: could not convert `0'' to `Map::Cell&''

Share this post


Link to post
Share on other sites
Well I might just use pointers then... But it feels so much cleaner to use references :-)

And I''ve never really used exception handling before. It just feels unnecessary.

Share this post


Link to post
Share on other sites
References must always reference an object, if you have the situation where you might need to return NULL then use return a pointer instead of a reference as has been mentined and let the caller determine is the pointer is valid

My 2D game engine

Share this post


Link to post
Share on other sites
Could you explicitly cast the NULL? Something like
return *(Cell**)(&NULL). I''m not sure, so don''t quote me on it, but I think you should be able to ::Somehow:: cast it to your type.

Share this post


Link to post
Share on other sites

  • 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!