Archived

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

robinei

NULL reference..

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
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
petewood mentioned the null object but didn''t really explain it.

You have a atatic object that is never used for anything, called the "error" or "zombie" or "loser" object. Then if you want to return null, you return the loser object. Then, the function that gets the result does an equality check, to see if it got the loser object.

Kind of like that Despair, Inc poster that says "It Could Be That The Purpose Of Your Life Is To Serve As a Warning To Others"

Share this post


Link to post
Share on other sites
quote:
Original post by robinei
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?


Throw an exception.
Be very careful when returning references. Make sure that what you return is not a temporary object. Only return a reference if you''re sure you need it.

Share this post


Link to post
Share on other sites
quote:
Original post by ms291052
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.

You certainly can

return static_cast<Cell**>(0);
// or

return static_cast<Cell**>(NULL);
// or with the old style cast

return Cell**(0);
// or maybe this

return (Cell**)0;
// this will also work I think

return (Cell**)(0);

All those statements will cast a 0 to be of type pointer to pointer to Cell and return it.
I don't think you can take the address of 0 (&NULL).

[edit]code to source tags[/edit]

[edited by - Rule on January 8, 2004 5:28:26 AM]

Share this post


Link to post
Share on other sites