NULL reference..
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?
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.
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&''
Just returning NULL I get this:
mapview.cpp:35: could not convert `0'' to `Map::Cell&''
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.
And I''ve never really used exception handling before. It just feels unnecessary.
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
My 2D game engine
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement