On 8/14/2010 8:51 AM, Alex yu wrote:
> provided with a struct:
> struct Person{
> char name[10];
> unsigned int id;
> };
>
> and a function :
> Person get_person( int)
> if the check in get_person failed, what should get_person return?
> for example
> if(int<0){
> return #Oh, what should I return?
> }
>
Aside from other people's suggestions, you could also throw an
exception. While from a theoretical standpoint I don't always agree with
that method of indicating "no value," it makes sense sometimes.
Another option would be to have two method calls: the first determines
the object's existence, the next gets the object. That is more
error-prone, but avoids the technical issue of a null object on the stack.
Finally, there is the option of having a dummy value. While geared
toward a different issue, this FAQ is still relevant:
http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2
--
John Gaughan
http://www.johngaughan.net/