I would say you have three options:
- return 0
- throw an exception
- use the Null object pattern
1) Has the drawback that you have to test for 0 if you want to avoid exceptions. This is not the way to go if you write modern C++ code as it makes the code jumpy (a lot of if's) and exceptions would make the code slow if the case where 0 is returned in not that likely (then it is not really an exception ;-) )
2) Same thing, only a way to go if the situation is unlikely
3) This will make the code work in all cases, only where you have to check if there are drugs or not, you have to compare to the defined NullObject, not to 0. This could alternatively also be solved by using std::shared_ptr
(which has become part of the new C++ standard lib, if you have an older STL, you can use boost::shared_ptr
, it is a header only template class)
FYI: I gathered the design patterns I could find and put them into an overviewable list
http://www.color-of-code.de/index.php?option=com_content&view=article&id=68:software-patterns&catid=43:design&Itemid=66
There are backlinks to wikipedia for the entries I could find.
EDIT: Here the link for the Null Object Pattern only: http://en.wikipedia.org/wiki/Null_Object_pattern