NULL reference..

Started by
12 comments, last by robinei 20 years, 3 months ago
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"
Advertisement
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.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
quote:Original post by pinacolada
petewood mentioned the null object but didn''t really explain it.


googling for nullobject or null object brings up plenty of material
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);// orreturn static_cast<Cell**>(NULL);// or with the old style castreturn Cell**(0);// or maybe thisreturn (Cell**)0;// this will also work I thinkreturn (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]

This topic is closed to new replies.

Advertisement